Ios 为什么dequeueReusableCellWithIdentifier在单元alloc后返回nil?
我正在我的Ios 为什么dequeueReusableCellWithIdentifier在单元alloc后返回nil?,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我正在我的UITableView中使用一个customUITableViewCell。问题是“dequeueReusableCellWithIdentifier”在返回一个已经创建的单元格时返回nil,至少我认为是这样 代码如下: ...... self.chattableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, width, height) style:UITableViewStylePlain]; sel
UITableView
中使用一个custom
UITableViewCell
。问题是“dequeueReusableCellWithIdentifier
”在返回一个已经创建的单元格时返回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
......
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
......
似乎“dequeueReusableCellWithIdentifier
”有时返回nil
,即使已经创建了具有相同标识符的单元格。
为什么会这样
在我离开之后
[cell SetCellWithReuseIdentifier:reusestr andCellData:tempcelldata];
似乎更好,以下是打印信息:
......
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
......
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
......
当然,删除函数后,显示的数据现在不正确。它似乎不会重用具有不同帧的单元,即使它们具有相同的重用标识符。从
dequeueReusableCellWithIdentifier
:获得nil是完全正常的,并且应为。如果为nil,则必须创建单元格实例。如果您注册了一个单元格,请使用dequeueReusableCellWithIdentifier:forIndexPath
:。这不会返回nil从dequeueReusableCellWithIdentifier
:获取nil是完全正常的,预期的。如果为nil,则必须创建单元格实例。如果您注册了一个单元格,请使用dequeueReusableCellWithIdentifier:forIndexPath
:。这不会返回nil首先,必须在viewdidload方法中注册该tableview单元格,如下所示:
[YOURTABLEVIEW registerNib:[UINib nibWithNibName:@"YOURTABLEVIEWCELL" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"TABLEVIEW CELL IDENTIFIER"];
希望这对您有所帮助:)首先,您必须在viewdidload方法中注册tableview单元格,如下所示:
[YOURTABLEVIEW registerNib:[UINib nibWithNibName:@"YOURTABLEVIEWCELL" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"TABLEVIEW CELL IDENTIFIER"];
希望这对您有所帮助:)出列…方法尝试查找当前屏幕外具有给定重用标识符的单元格。如果找到一个,则返回该单元格,否则返回零。因此,如果他们可以将单元格出列,这取决于许多因素:单元格高度与表视图的比率,表视图中出列标识符不同值的分布,是否创建了具有该标识符的单元格,等等,从该方法中获取nil是完全正常的。出列…方法尝试查找当前屏幕外具有给定重用标识符的单元格。如果找到一个,则返回该单元格,否则返回零。因此,如果他们可以将单元格出列,这取决于许多因素:单元格高度与表视图的比率,表视图中出列标识符不同值的分布,是否创建了具有该标识符的单元格,等等,从该方法中获得nil是完全正常的。这里您每次都在更改dequeueReusableCellWithIdentifier 提供从stroybord提供的标识符名称并使用它们
static NSString *cellIdentifier = @"cellidentifier";
ChatTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
在这里,您每次都在更改dequeueReusableCellWithIdentifier 提供从stroybord提供的标识符名称并使用它们
static NSString *cellIdentifier = @"cellidentifier";
ChatTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
在方法中尝试以下代码 UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:@“MyIdentifier”]
在方法中尝试以下代码 UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:@“MyIdentifier”]
Ilongwu:不太确定,请删除[cell SetCellWithReuseIdentifier:reusestr和CellData:tempcelldata];在indexpath的行的单元格中,再次检查结果:)此语句没有多大意义:)IlongWu:不太确定是否可以删除[cell SetCellWithReuseIdentifier:reusestr和CellData:tempcelldata];在indexpath的行中输入单元格,并再次检查结果:)这句话没有多大意义:)我认为这是OP的观点-他们正试图测试在给定一系列不同重用标识符的情况下创建单元格的方式。他根据其模型对象的一个属性为单元格提供不同的标识符,这使得有意义。我认为这是OP的观点-他们正试图测试给定一系列不同重用标识符的单元格创建方式。他根据模型对象的属性为单元格提供了一个完全有意义的标识符。我认为OP的观点是他们正试图测试给定重用标识符的单元格创建方式不同的重用标识符我认为OP的重点是,他们试图测试在给定不同重用标识符的情况下创建单元的方式