2010-04-22 56 views
1

我想在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]; 
} 

回答

1

我在這裏看到了兩個問題:

1)您使用的encodeObject:序列化您的dataArray但省略反序列化它initWithCoder:

2),你應該RA使用NSKeyedArchiver的方法encodeObject:forKey:。然後您可以自由地在解碼中使用序列化信息。

除此之外,我只能說回答你的問題,這將是有益的,包括有關崩潰發生的地方的信息。

+0

感謝您的回答。我會先嚐試反序列化。但關於崩潰,正如我已經提到的:但是,當我initWithCoder,UITableViewController仍然正確加載,但當我點擊一個單元格時,應用程序崩潰和調試器告訴我有關EXEC_BAD_ACCESS,我的內存有問題,但我不知道 – vodkhang 2010-04-22 10:36:54

+0

你可以使用這些推薦工作來發布你的新代碼嗎?謝謝 – snapfish 2014-03-02 01:09:45

1

此代碼中只有一個內存問題:在init您正在泄漏NSMutableArray。但是,這是你正在尋找的相反問題。您所描述的錯誤不在發佈的代碼中。