dequeueReusableCellWithIdentifier为同一索引路径返回不同的单元格

时间:2018-04-26 14:05:12

标签: ios uitableview

我正在尝试了解出现了什么问题,因为当我执行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

2 个答案:

答案 0 :(得分:1)

  

cellForRowAtIndexPath被调用,dequeueReusableCellWithIdentifier没有得到我需要的单元格

您误解了表视图的工作原理。细胞被重复使用。现在可以在一行中使用相同的单元格对象,现在在另一行中。一个单元对象现在可以占用相同的行,现在由另一个单元对象占用。没有通信。这完全取决于您,并且完全无关,您获取特定索引路径的单元格。您的工作是配置您获得的任何单元格对象,以便该索引路径的正确

答案 1 :(得分:0)

因此,如果您想要获取确切的单元格,请为该单元格使用唯一的cellReuseIdentifier,然后它将阻止该单元格被重用。但是对于UITableView,单元重用是正常的,应该正确实现,因此视图不会占用太多内存(因为屏幕外的单元格将重新用于新内容)。您应该关心的只是如何清理单元格并使用新数据再次装饰它,而不是仅仅将旧单元格放回同一个indexPath。