我想在UITableViewController中編碼一些數據狀態。在第一次,我用Nibname初始化對象沒有任何問題。然而,當我的initWithCoder中,UITableViewController中仍然加載,但是當我點擊一個細胞,應用程序崩潰和調試器告訴我關於EXEC_BAD_ACCESS,什麼毛病我的記憶,但我不知道initWithCoder和initWithNibName
這裏是我的代碼:
- (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];
}
感謝您的回答。我會先嚐試反序列化。但關於崩潰,正如我已經提到的:但是,當我initWithCoder,UITableViewController仍然正確加載,但當我點擊一個單元格時,應用程序崩潰和調試器告訴我有關EXEC_BAD_ACCESS,我的內存有問題,但我不知道 – vodkhang 2010-04-22 10:36:54
你可以使用這些推薦工作來發布你的新代碼嗎?謝謝 – snapfish 2014-03-02 01:09:45