为什么dequeueReusableCellWithIdentifier在单元格分配后返回nil?

时间:2016-05-03 07:09:32

标签: ios objective-c uitableview

我在custom中使用UITableViewCell UITableView。问题是'dequeueReusableCellWithIdentifier'returns nil应该返回已经创建的单元格,至少我是这么认为的。

以下是代码:

......

    self.chattableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, width, height) style:UITableViewStylePlain];
    self.chattableView.separatorStyle=UITableViewCellSeparatorStyleNone;
    self.chattableView.dataSource=self;
    self.chattableView.delegate=self;
    [self.view addSubview:self.chattableView];

......


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    chatCellData* tempcelldata = self.cellDataArray[indexPath.row];
    NSString* reusestr =[NSString stringWithFormat:@"%d",tempcelldata.chattype];
    ChatTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reusestr];

    if(cell)//set cell data and change some label frame
    {
        [cell SetCellWithReuseIdentifier:reusestr andCellData:tempcelldata];
        NSLog(@"dequeueReusableCellWithIdentifier : %@ ",reusestr);
    }
    else//alloc cell
    {
        cell = [[ChatTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reusestr withCellData:tempcelldata];
        NSLog(@"alloc with Identifier:%@ ",reusestr);
    }
    return cell;
}

当我滚动tableview时,我得到以下打印信息:

......
Demo[40891:4326063] dequeueReusableCellWithIdentifier : 4 
Demo[40891:4326063] dequeueReusableCellWithIdentifier : 4
Demo[40891:4326063] alloc with Identifier:1 
Demo[40891:4326063] alloc with Identifier:5 
Demo[40891:4326063] alloc with Identifier:4 
Demo[40891:4326063] alloc with Identifier:4 
Demo[40891:4326063] alloc with Identifier:4 
Demo[40891:4326063] alloc with Identifier:2 
Demo[40891:4326063] alloc with Identifier:1 
Demo[40891:4326063] alloc with Identifier:5 
Demo[40891:4326063] alloc with Identifier:2 
Demo[40891:4326063] alloc with Identifier:32 
Demo[40891:4326063] alloc with Identifier:1 
Demo[40891:4326063] alloc with Identifier:1 
Demo[40891:4326063] alloc with Identifier:32 
Demo[40891:4326063] alloc with Identifier:4 
Demo[40891:4326063] dequeueReusableCellWithIdentifier : 4 
Demo[40891:4326063] alloc with Identifier:32 
Demo[40891:4326063] alloc with Identifier:4 
Demo[40891:4326063] alloc with Identifier:4 
Demo[40891:4326063] alloc with Identifier:4 
Demo[40891:4326063] dequeueReusableCellWithIdentifier : 32 
Demo[40891:4326063] alloc with Identifier:1 
Demo[40891:4326063] alloc with Identifier:32 
Demo[40891:4326063] dequeueReusableCellWithIdentifier : 1 
Demo[40891:4326063] alloc with Identifier:1 
Demo[40891:4326063] alloc with Identifier:32 
Demo[40891:4326063] alloc with Identifier:1 
Demo[40891:4326063] alloc with Identifier:4 
Demo[40891:4326063] alloc with Identifier:32 
Demo[40891:4326063] alloc with Identifier:4 
Demo[40891:4326063] alloc with Identifier:32 
Demo[40891:4326063] alloc with Identifier:4 
Demo[40891:4326063] alloc with Identifier:32 
Demo[40891:4326063] alloc with Identifier:4 
Demo[40891:4326063] alloc with Identifier:32 
Demo[40891:4326063] alloc with Identifier:4 
Demo[40891:4326063] alloc with Identifier:32 
Demo[40891:4326063] alloc with Identifier:4 
Demo[40891:4326063] alloc with Identifier:32 
Demo[40891:4326063] alloc with Identifier:4 
Demo[40891:4326063] alloc with Identifier:32 
Demo[40891:4326063] alloc with Identifier:4

有时,即使已经创建了具有相同标识符的单元格,“dequeueReusableCellWithIdentifierreturns nil也是如此。 这是为什么?

删除后

 [cell SetCellWithReuseIdentifier:reusestr andCellData:tempcelldata];

似乎更好,这是打印信息:

......
Demo[40999:4371081] dequeueReusableCellWithIdentifier : 4 
Demo[40999:4371081] dequeueReusableCellWithIdentifier : 4 
Demo[40999:4371081] dequeueReusableCellWithIdentifier : 4 
Demo[40999:4371081] dequeueReusableCellWithIdentifier : 4 
Demo[40999:4371081] dequeueReusableCellWithIdentifier : 4 
Demo[40999:4371081] dequeueReusableCellWithIdentifier : 32 
Demo[40999:4371081] dequeueReusableCellWithIdentifier : 4 
Demo[40999:4371081] dequeueReusableCellWithIdentifier : 4 
Demo[40999:4371081] dequeueReusableCellWithIdentifier : 4 
Demo[40999:4371081] dequeueReusableCellWithIdentifier : 4 
Demo[40999:4371081] dequeueReusableCellWithIdentifier : 4 
Demo[40999:4371081] dequeueReusableCellWithIdentifier : 4 
Demo[40999:4371081] dequeueReusableCellWithIdentifier : 4 
Demo[40999:4371081] dequeueReusableCellWithIdentifier : 32 
Demo[40999:4371081] dequeueReusableCellWithIdentifier : 4 
Demo[40999:4371081] dequeueReusableCellWithIdentifier : 4 
Demo[40999:4371081] dequeueReusableCellWithIdentifier : 4 
Demo[40999:4371081] dequeueReusableCellWithIdentifier : 32 
Demo[40999:4371081] dequeueReusableCellWithIdentifier : 1 
Demo[40999:4371081] dequeueReusableCellWithIdentifier : 1 
Demo[40999:4371081] dequeueReusableCellWithIdentifier : 32 
Demo[40999:4371081] dequeueReusableCellWithIdentifier : 1 
Demo[40999:4371081] dequeueReusableCellWithIdentifier : 32 
Demo[40999:4371081] dequeueReusableCellWithIdentifier : 1 
Demo[40999:4371081] dequeueReusableCellWithIdentifier : 32 
Demo[40999:4371081] dequeueReusableCellWithIdentifier : 1 
Demo[40999:4371081] dequeueReusableCellWithIdentifier : 1 
Demo[40999:4371081] dequeueReusableCellWithIdentifier : 32 
Demo[40999:4371081] dequeueReusableCellWithIdentifier : 1 
Demo[40999:4371081] dequeueReusableCellWithIdentifier : 32 
Demo[40999:4371081] dequeueReusableCellWithIdentifier : 4 
Demo[40999:4371081] dequeueReusableCellWithIdentifier : 4 
Demo[40999:4371081] dequeueReusableCellWithIdentifier : 4 
Demo[40999:4371081] dequeueReusableCellWithIdentifier : 32 
Demo[40999:4371081] dequeueReusableCellWithIdentifier : 4 
Demo[40999:4371081] dequeueReusableCellWithIdentifier : 4 
Demo[40999:4371081] dequeueReusableCellWithIdentifier : 32 
Demo[40999:4371081] dequeueReusableCellWithIdentifier : 1 
Demo[40999:4371081] dequeueReusableCellWithIdentifier : 1 
Demo[40999:4371081] dequeueReusableCellWithIdentifier : 32 
Demo[40999:4371081] dequeueReusableCellWithIdentifier : 2 
Demo[40999:4371081] dequeueReusableCellWithIdentifier : 5 
Demo[40999:4371081] dequeueReusableCellWithIdentifier : 1 
Demo[40999:4371081] dequeueReusableCellWithIdentifier : 4 
Demo[40999:4371081] dequeueReusableCellWithIdentifier : 4 
Demo[40999:4371081] dequeueReusableCellWithIdentifier : 1 
Demo[40999:4371081] dequeueReusableCellWithIdentifier : 5 
Demo[40999:4371081] dequeueReusableCellWithIdentifier : 4 
Demo[40999:4371081] dequeueReusableCellWithIdentifier : 4 
Demo[40999:4371081] dequeueReusableCellWithIdentifier : 4 
Demo[40999:4371081] dequeueReusableCellWithIdentifier : 2 
Demo[40999:4371081] dequeueReusableCellWithIdentifier : 1 
Demo[40999:4371081] dequeueReusableCellWithIdentifier : 5 
Demo[40999:4371081] dequeueReusableCellWithIdentifier : 2 
Demo[40999:4371081] dequeueReusableCellWithIdentifier : 32 
Demo[40999:4371081] dequeueReusableCellWithIdentifier : 1 
Demo[40999:4371081] dequeueReusableCellWithIdentifier : 1 
Demo[40999:4371081] dequeueReusableCellWithIdentifier : 32 
Demo[40999:4371081] dequeueReusableCellWithIdentifier : 4 
Demo[40999:4371081] dequeueReusableCellWithIdentifier : 4 
Demo[40999:4371081] dequeueReusableCellWithIdentifier : 32 
Demo[40999:4371081] dequeueReusableCellWithIdentifier : 4 
Demo[40999:4371081] dequeueReusableCellWithIdentifier : 4 
Demo[40999:4371081] dequeueReusableCellWithIdentifier : 4 
Demo[40999:4371081] dequeueReusableCellWithIdentifier : 32 
Demo[40999:4371081] dequeueReusableCellWithIdentifier : 32 
Demo[40999:4371081] dequeueReusableCellWithIdentifier : 2 
......

当然,删除该功能后显示的数据现在不正确。它似乎不会重用具有不同帧的单元,即使它们具有相同的重用标识符。

5 个答案:

答案 0 :(得分:4)

dequeue ...方法尝试查找具有当前 offscreen 的给定重用标识符的单元格。如果他们找到一个,他们会返回那个单元格,否则他们返回零。因此,如果它们可以使单元格出列,则取决于许多因素:单元格高度与表格视图的比率,表格视图中出列标识符的不同值的分布,是否具有该标识符的单元格如上所述,从这种方法中得到零是完全正常的。

答案 1 :(得分:2)

在您的方法中尝试以下代码

UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@" MyIdentifier"];

/*
 *   If the cell is nil it means no cell was available for reuse and that we should
 *   create a new one.
 */
if (cell == nil) {

    /* 
     *   Actually create a new cell (with an identifier so that it can be dequeued). 
     */

    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"MyIdentifier"] autorelease];

    cell.selectionStyle = UITableViewCellSelectionStyleNone;

}

答案 2 :(得分:1)

dequeueReusableCellWithIdentifier获取nil:完全正常且预期。如果它为零,则必须创建单元实例。如果您注册了一个单元格,请使用dequeueReusableCellWithIdentifier:forIndexPath:那不会没有回复

答案 3 :(得分:0)

首先,您必须在viewdidload方法中注册该tableview单元格,如下所示:

[YOURTABLEVIEW registerNib:[UINib nibWithNibName:@"YOURTABLEVIEWCELL" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"TABLEVIEW CELL IDENTIFIER"];

希望这可以帮助你:)

答案 4 :(得分:0)

这里你每次都在改变dequeueReusableCellWithIdentifier

给出你从stroybord提供的标识符名称并使用它们

 static NSString *cellIdentifier = @"cellidentifier";
ChatTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];