【问题标题】:UITableView is loading every cellUITableView 正在加载每个单元格
【发布时间】:2009-09-01 13:54:09
【问题描述】:

UITableview 应该只加载一开始可见的单元格吗?我的表格视图最初会加载每个单元格,这会减慢它的速度。我正在使用大约 1000 行。只希望它在必须时加载一个单元格(比如用户向下滚动)。有人知道为什么要这样做吗?

【问题讨论】:

  • 请澄清;您的意思是 A) 您已将 NSLog 语句添加到您的 cellForRowAtIndexPath 方法并发现它被调用了 1000 次,或者 B) 它运行缓慢并且您假设该方法被调用了 1000 次?
  • 用动画重新加载数据会导致它加载每个单元格,有没有办法只使用 6 行的动画? [pTableView scrollRectToVisible:CGRectMake(0, 0, 1, 1) 动画:NO]; //[pTableView reloadData]; [pTableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:animDirection];
  • 对于它的价值,我也在我自己的项目中看到了这种行为 - [tableView reloadSections:...] 似乎让它运行 cellForRowAtIndexPath: for every 行,而不是只是可见的

标签: iphone uitableview iphone-sdk-3.0


【解决方案1】:

我知道 cellForRowAtIndexPath 最初是为每个单元格调用的。单元格的高度为 89。

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

static NSString *CellIdentifier = @"Cell";
UILabel *textName               = nil;
UIImageView* image              = nil;
unsigned int        DATA_TAG    = 1001;
unsigned int        IMG_TAG     = 1002;

//  Retrieve a cell is Available
cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier];

//  Check if no new cell was available
if (cell == nil) 
{
    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];

    //  Set the Accessory Data
    textName = [[[UILabel alloc]initWithFrame:CGRectMake(cell.frame.origin.x, 80, cell.frame.size.width, 20)]autorelease];
    textName.tag                    = DATA_TAG;
    textName.textAlignment          = UITextAlignmentCenter;
    textName.backgroundColor        = [UIColor clearColor];
    textName.highlightedTextColor   = [UIColor colorWithRed:1.0 green:1.0 blue:0.9 alpha:1.0];
    textName.textColor              = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1.0];
    textName.lineBreakMode          = UILineBreakModeWordWrap;

    [cell.contentView addSubview:textName];

    //Set the Image Data
    image = [[[UIImageView alloc]initWithFrame:CGRectMake(cell.frame.origin.x, cell.frame.origin.y, cell.frame.size.width, 80)]autorelease];
    image.tag = IMG_TAG;
    image.contentMode= UIViewContentModeScaleAspectFit;
    [cell.contentView addSubview:image];

}

Accessory* acc= [[AccessoryManager sharedManager].currentList objectAtIndex:indexPath.row];
if(acc == nil)
    return cell;


textName= (UILabel*)[cell.contentView viewWithTag:DATA_TAG];
textName.text= acc.accessoryName;


image= (UIImageView*)[cell.contentView viewWithTag:IMG_TAG];
[image setImage:acc.accessoryImage];    
return cell;
}

【讨论】:

  • 如果您要添加更多信息,您应该编辑原始问题,而不是发布您自己问题的“答案”...
  • 在哪里声明了单元格,为什么要调用 [aTableView dequeue.. 而不是 [tableView dequeue...
  • 回应@Rob 的担忧。另外,请发布您的- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 以供参考。
  • 我发现调用 [pTableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:animDirection] 正在重新加载所有单元格(所有单元格都在一个部分中)。我将如何只为那些可见的动画制作动画。
  • 这不应该发生。请参阅上面关于发布您的 -heightForRowAtIndexPath 的评论。此外,您可能希望编辑您的问题以在此“答案”中包含代码。我们通常看那里是为了回答问题,而不是在 cmets 中寻找答案。
【解决方案2】:

你能发布一些代码吗?方法 -tableView:cellForRowAtIndexPath 仅在潜在单元格的新“插槽”打开时才调用,因此您需要做一些重大错误才能开始“加载每个单元格”!

您的意思是说您最初是在加载所有数据,并希望分批加载吗?

【讨论】:

    【解决方案3】:

    没错 - 它应该只加载它必须加载的那些。你的UITableView 的rowHeight 是什么?如果该值非常低,则表格可能需要加载所有单元格

    如果这不是问题,您可以粘贴您的tableView:cellForRowAtIndexPath: 代码吗?

    【讨论】:

    • 即使每行 1 个像素(!),他仍然无法以该方法加载一半的单元格。这个问题肯定是关于加载数据的时间吗?
    • AFAIK,使 tableView:cellForRowAtIndexPath: 在第一次加载视图时被调用 1000 次的唯一方法是将 rowheight 设置为 0。
    【解决方案4】:

    您是否在自己调用 cellForRowAtIndexPath,例如从 heightForRowAtIndexPath 调用?如果是这样,您不需要创建单元格来确定其高度。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-22
      • 2021-03-21
      • 2013-10-15
      • 1970-01-01
      相关资源
      最近更新 更多