问题
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.
Here is the code:
......
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;
}
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
It seems that 'dequeueReusableCellWithIdentifier
' returns nil
sometimes even if the cell with the same Identifier have already been created.
Why is this ?
after I remove
[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.
回答1:
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.
回答2:
Try the following code into your method
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;
}
回答3:
First You have to register that tableview cell in your viewdidload method like this :
[YOURTABLEVIEW registerNib:[UINib nibWithNibName:@"YOURTABLEVIEWCELL" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"TABLEVIEW CELL IDENTIFIER"];
Hope this help you :)
回答4:
Getting nil from dequeueReusableCellWithIdentifier
: is perfectly normal and expected. If it's nil you must create the cell instance. If you registered a cell then use dequeueReusableCellWithIdentifier:forIndexPath
:. That won't return nil
回答5:
here you are changing dequeueReusableCellWithIdentifier every time
give the identifier name that you given from stroybord and use them
static NSString *cellIdentifier = @"cellidentifier";
ChatTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
来源:https://stackoverflow.com/questions/36997857/why-does-dequeuereusablecellwithidentifier-return-nil-after-cell-alloc