为什么 dequeueReusableCellWithIdentifier 在单元分配后返回 nil? [英] Why does dequeueReusableCellWithIdentifier return nil after cell alloc?

查看:30
本文介绍了为什么 dequeueReusableCellWithIdentifier 在单元分配后返回 nil?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using a custom UITableViewCell in my UITableView. The problem is that 'dequeueReusableCellWithIdentifier' returns nil when it should return a already created cell,at least I think so.

代码如下:

......

    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 时,我得到以下打印信息:

When I scroll the tableview I get the following print info:

......
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

似乎 'dequeueReusableCellWithIdentifier' return nil 有时即使已经创建了具有相同标识符的单元格.这是为什么?

It seems that 'dequeueReusableCellWithIdentifier' returns nil sometimes even if the cell with the same Identifier have already been created. Why is this ?

在我删除之后

 [cell SetCellWithReuseIdentifier:reusestr andCellData:tempcelldata];

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

It seems to be better, here is the print info:

......
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 
......

当然,删除功能后显示的数据现在是不正确的.看来即使重用标识符相同,它也不会重用具有不同帧的单元格.

of course the showed data is not correct now after remove the function. It seems that it will not reuse the cell with different frame even they have the same reuse Identifier.

推荐答案

dequeue… 方法尝试查找具有给定重用标识符的单元格,该单元格当前屏幕外.如果他们找到一个,他们返回那个单元格,否则他们返回 nil.因此,如果它们可以使单元格出列,则取决于很多事情:单元格的高度与表格视图的比率,表格视图中出列标识符的不同值的分布,具有该标识符的单元格是否曾经created 等等.正如前面所说,从那个方法中得到 nil 是完全正常的.

The dequeue… methods try to find a cell with the given reuse identifier that is currently offscreen. If they find one, they return that cell, otherwise they return nil. Thus it depends on a numerous thing if they can dequeue a cell: the ratio of the height of the cell and the table view, the distribution of the different values of the dequeue identifiers in your table view, whether a cell with that identifier was ever created, etc. etc. As it was said, it is completely normal to get nil from that method.

这篇关于为什么 dequeueReusableCellWithIdentifier 在单元分配后返回 nil?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆