我正在尝试在UITableViewController中编码一些数据状态。第一次,我用Nibname初始化对象,没有任何问题。但是,当我使用initWithCoder时,UITableViewController仍然加载,但是当我单击一个单元时,应用程序崩溃,调试器告诉我有关EXEC_BAD_ACCESS的信息,这是我的内存出了问题,但我不知道
下面是我的代码:
- (id) init {
if(self = [self initWithNibName:@"DateTableViewController" bundle:nil]) {
self.dataArray = [[NSMutableArray alloc] initWithObjects:@"1", @"2", @"3", nil];
}
return self;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Set up the cell...
int index = indexPath.row;
cell.textLabel.text = [self.dataArray objectAtIndex:index];;
return cell;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return @"Test Archiver";
}
- (void)encodeWithCoder:(NSCoder *)coder {
[super encodeWithCoder:coder];
[coder encodeObject:self.dataArray];
}
- (id)initWithCoder:(NSCoder *)coder {
return [self init];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
int index = indexPath.row;
[self.dataArray addObject:[NSString stringWithFormat:@"Hello %d", index]];
[self.tableView reloadData];
}发布于 2010-04-22 16:16:01
我在这里看到两个问题:
1)您正在使用encodeObject:序列化dataArray,但忽略了在initWithCoder:中对其进行反序列化
2)你应该使用NSKeyedArchiver的encodeObject:forKey:方法,这样你就可以在解码时自由使用序列化的信息。
除此之外,我只能说,为了回答您的问题,包括关于崩溃发生的位置的信息将是有帮助的。
发布于 2010-04-23 02:25:36
这段代码中只有一个内存问题:在init中,您会泄漏NSMutableArray。但这与您正在寻找的问题正好相反。您描述的错误不在发布的代码中。
https://stackoverflow.com/questions/2688903
复制相似问题