0

UITableViewController crashes with a exc bad accesses error. This is my code:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    NSString *cellText ;
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ;
    }
    if (indexPath.row == 0) 
    {
        cellText = @"Mail1";
    }
    else if (indexPath.row == 1) 
    {
        cellText = @"Mail2";
    }
    else if (indexPath.row == 2)
    {
        cellText = @"Mail3";
    }
    else if(indexPath.row == 3)
    {
        cellText = @"Mail4";
    }
    else
    {
        cellText = @"Mail5";
    }
    cell.textLabel.text = cellText;
    return cell;
}

this is the log message xcode gave

XXXXProject[1366:f803] * -[XXXXViewController tableView:cellForRowAtIndexPath:]: message sent to deallocated instance 0x6a793f0

4

2 回答 2

0

我认为您不必初始化单元格。也许故事板初始化单元格。

ss

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    // Configure the cell...
    NSString *cellText;
    if (indexPath.row == 0) 
    {
        cellText = @"Mail1";
    }
    else if (indexPath.row == 1) 
    {
        cellText = @"Mail2";
    }
    else if (indexPath.row == 2)
    {
        cellText = @"Mail3";
    }
    else if(indexPath.row == 3)
    {
        cellText = @"Mail4";
    }
    else
    {
        cellText = @"Mail5";
    }
    cell.textLabel.text = cellText;
    return cell;
}

您可以从 GitHub 下载此示例项目,然后运行它。

https://github.com/weed/p120817_TableTest

于 2012-08-17T09:49:39.940 回答
0

几个小时前刚刚在 StackOverflow 上发布了完全相同的问题。(没有一个表视图称为它的委托,而是一个 UIButton 它的动作)。问题可能是您没有对表视图控制器的强引用,因此它在创建后立即被释放。请参阅我对上一个问题的回答。

于 2012-08-17T11:54:55.583 回答