【发布时间】:2018-04-26 14:05:11
【问题描述】:
我试图了解出了什么问题,因为当我进行 tableview 更新并调用 cellForRowAtIndexPath 时,dequeueReusableCellWithIdentifier 没有取回我需要的单元格。
只是表格视图更新,indexPath完全相同,dequeueReusableCellWithIdentifier在第一次更新时返回nil并为其创建另一个单元格。这很糟糕,因为每个更新方法都调用了两次,导致图像闪烁
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if (cell == nil)
cell = [[cellClass alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID];
if ([cellClass isEqual:[ATTimeLineMealCell class]]) {
NSLog(@"table view: %@",tableView);
NSLog(@"CELL DATA: %@",cell);
NSLog(@"Row: %ld , section: %ld",indexPath.row,indexPath.section);
}
}
- (void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"table view: %@",tableView);
NSLog(@"CELL DATA UPDATE:%@",cell);
}
流程: 插入新单元格 然后在单元格上重新加载(不是 reloadData)
还有日志:
> 2018-04-26 15:53:26.214828+0200 Ate[6956:2981607] table view:
> <ATQueryTableView: 0x10482a600; baseClass = UITableView; frame = (0 0;
> 375 667); clipsToBounds = YES; gestureRecognizers = <NSArray:
> 0x1c044f960>; layer = <CALayer: 0x1c00362a0>; contentOffset: {0,
> -292}; contentSize: {375, 747}; adjustedContentInset: {292, 0, 0, 0}> 2018-04-26 15:53:26.215271+0200 Ate[6956:2981607] CELL DATA:
> <ATTimeLineMealCell: 0x1040aa800; baseClass = UITableViewCell; frame =
> (0 0; 375 44); layer = <CALayer: 0x1c4437de0>> 2018-04-26
> 15:53:26.215290+0200 Ate[6956:2981607] Row: 1 , section: 0 2018-04-26
> 15:53:26.216296+0200 Ate[6956:2981607] table view: <ATQueryTableView:
> 0x10482a600; baseClass = UITableView; frame = (0 0; 375 667);
> clipsToBounds = YES; gestureRecognizers = <NSArray: 0x1c044f960>;
> layer = <CALayer: 0x1c00362a0>; contentOffset: {0, -292}; contentSize:
> {375, 747}; adjustedContentInset: {292, 0, 0, 0}> 2018-04-26
> 15:53:26.216409+0200 Ate[6956:2981607] CELL DATA
> UPDATE:<ATTimeLineMealCell: 0x1040aa800; baseClass = UITableViewCell;
> frame = (0 80; 375 120); autoresize = W; layer = <CALayer:
> 0x1c4437de0>>
>
> 2018-04-26 15:53:28.456177+0200 Ate[6956:2981607] table view:
> <ATQueryTableView: 0x10482a600; baseClass = UITableView; frame = (0 0;
> 375 667); clipsToBounds = YES; gestureRecognizers = <NSArray:
> 0x1c044f960>; layer = <CALayer: 0x1c00362a0>; contentOffset: {0, 80};
> contentSize: {375, 747}; adjustedContentInset: {315, 0, 0, 0}>
> 2018-04-26 15:53:28.456308+0200 Ate[6956:2981607] CELL DATA:
> <ATTimeLineMealCell: 0x104162a00; baseClass = UITableViewCell; frame =
> (0 0; 375 44); layer = <CALayer: 0x1c4437bc0>> 2018-04-26
> 15:53:28.456387+0200 Ate[6956:2981607] Row: 1 , section: 0 2018-04-26
> 15:53:28.457095+0200 Ate[6956:2981607] table view: <ATQueryTableView:
> 0x10482a600; baseClass = UITableView; frame = (0 0; 375 667);
> clipsToBounds = YES; gestureRecognizers = <NSArray: 0x1c044f960>;
> layer = <CALayer: 0x1c00362a0>; contentOffset: {0, 80}; contentSize:
> {375, 747}; adjustedContentInset: {315, 0, 0, 0}> 2018-04-26
> 15:53:28.457191+0200 Ate[6956:2981607] CELL DATA
> UPDATE:<ATTimeLineMealCell: 0x104162a00; baseClass = UITableViewCell;
> frame = (0 80; 375 120); autoresize = W; layer = <CALayer:
> 0x1c4437bc0>>
>
>
> 2018-04-26 15:53:29.814609+0200 Ate[6956:2981607] table view:
> <ATQueryTableView: 0x10482a600; baseClass = UITableView; frame = (0 0;
> 375 667); clipsToBounds = YES; gestureRecognizers = <NSArray:
> 0x1c044f960>; layer = <CALayer: 0x1c00362a0>; contentOffset: {0, 80};
> contentSize: {375, 747}; adjustedContentInset: {315, 0, 0, 0}>
> 2018-04-26 15:53:29.816399+0200 Ate[6956:2981607] CELL DATA:
> <ATTimeLineMealCell: 0x1040aa800; baseClass = UITableViewCell; frame =
> (0 80; 375 120); hidden = YES; autoresize = W; layer = <CALayer:
> 0x1c4437de0>> 2018-04-26 15:53:29.816420+0200 Ate[6956:2981607] Row: 1
> , section: 0 2018-04-26 15:53:29.816792+0200 Ate[6956:2981607] table
> view: <ATQueryTableView: 0x10482a600; baseClass = UITableView; frame =
> (0 0; 375 667); clipsToBounds = YES; gestureRecognizers = <NSArray:
> 0x1c044f960>; layer = <CALayer: 0x1c00362a0>; contentOffset: {0, 80};
> contentSize: {375, 747}; adjustedContentInset: {315, 0, 0, 0}>
> 2018-04-26 15:53:29.816849+0200 Ate[6956:2981607] CELL DATA
> UPDATE:<ATTimeLineMealCell: 0x1040aa800; baseClass = UITableViewCell;
> frame = (0 80; 375 120); autoresize = W; layer = <CALayer:
> 0x1c4437de0>>
从日志看来,当更新到来时,它会为同一个 indexPath 再创建一个单元格:
- 原始单元内存地址:0x1040aa800
- 新生成的但是为什么?内存地址:0x104162a00
【问题讨论】:
-
您的代码不是真正的代码,因为您没有返回单元格。 显示真实代码。另外,您正在调用错误的方法。不要不要致电
dequeueReusableCellWithIdentifier:!致电dequeueReusableCellWithIdentifier:forIndexPath:。最后,单元格被重复使用,因此没有“我需要的单元格”之类的东西;你得到你得到的单元格,你的工作是为要在表格中使用它的索引路径准备它。 -
这会将单元格设置为一个新的内存地址,该地址将返回该特定的索引路径,因此会有所不同。
-
您担心的事实是问题。您完全误解了表格视图及其单元格的工作方式。细胞被重复使用。单元格“内存地址”和索引路径之间没有对应关系。同一个单元格可以出现在不同的行中。不同的单元格可以出现在同一行中。
-
我知道可重复使用的单元格,但绝对不明白我的表格视图中是否只有一行,为什么每次更新都会调用两次,对于同一索引路径中的两个单元格
标签: ios uitableview