0

我试过了,但在 cellforrowatindexpath 出现异常错误

下面是我得到的例外。

-[UITableView _createPreparedCellForGlobalRow:withIndexPath:] 中的断言失败,/SourceCache/UIKit_Sim/UIKit-1914.84

if(aTableView==specTable)
{
    static NSString *CellIdentifier = @"cell";
    UITableViewCell *cell = [specTable dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell==nil)
    {
        cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2
                                    reuseIdentifier:CellIdentifier];

    }

    return cell;

} 
else 
{    
    static NSString *CellIdentifier2 = @"cell2";
    UITableViewCell *cell= [table2     dequeueReusableCellWithIdentifier:ReviewCellIdentifier2];
}

return cell;
4

1 回答 1

0

两个问题:

  1. 你永远不会回来cell2。在此方法结束时cell,无论发送者表视图是等于第一个还是第二个,您总是返回。
  2. else如果在第二部分(分支)中dequeueReusableCellWithIdentifier:消息返回,则不会像在第一部分中那样创建单元格nil

总而言之:

if (aTableView == specTable)
{
    static NSString *cellIdentifier = @"cell";
    UITableViewCell *cell = [specTable dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2
                                    reuseIdentifier:CellIdentifier] autorelease]; // you were also leaking memory here

    }

    return cell;

} 
else 
{    
    static NSString *cellIdentifier2 = @"cell2";
    UITableViewCell *cell2 = [table2 dequeueReusableCellWithIdentifier:cellIdentifier2];
    if (cell2 == nil)
    {
        cell2 = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2
                                    reuseIdentifier:CellIdentifier] autorelease];

    }
    return cell2;
}

return nil; // just to make the compiler happy
于 2012-08-01T08:02:43.513 回答