2

我正在使用 UITableView 来显示使用 Interface Builder 创建的自定义单元格。表格滚动不是很流畅(它“口吃”),这让我相信单元格没有被重用。这段代码有问题吗?

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

static NSString *CellIdentifier = @"TwitterCell";
TwitterCell *cell = (TwitterCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil){
    //new cell
    NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"TwitterCell" 
                                                   owner:nil options:nil];

    for(id currentObject in topLevelObjects)
    {
        if([currentObject isKindOfClass:[TwitterCell class]])
        {
            cell = (TwitterCell *)currentObject;
            break;
        }
    }

}

if([self.tweets count] > 0) {
    cell.lblUser.text = [[self.tweets objectAtIndex:[indexPath row]] username];
    cell.lblTime.text = [[self.tweets objectAtIndex:[indexPath row]] time];
    [cell.txtText setText:[[self.tweets objectAtIndex:[indexPath row]] text]];
    [[cell imgImage] setImage:[[self.tweets objectAtIndex:[indexPath row]] image]];
} else {
    cell.txtText.text = @"Loading...";
}


cell.selectionStyle = UITableViewCellSelectionStyleNone;

return cell;
}
4

4 回答 4

6

也许 atebits(Tweetie 的创建者)的这篇博客文章可以帮助你:http ://blog.atebits.com/2008/12/fast-scrolling-in-tweetie-with-uitableview/

切入正题,秘诀如下:每个表格单元格有一个自定义视图,并进行自己的绘图。听起来很简单?那是因为它是。它实际上比处理大量标签和图像的子视图更简单,而且速度要快上亿倍(根据我的非正式测试)。

于 2010-03-26T22:16:29.710 回答
2

几件事:

  1. 确保 IB 中的单元格标识符与您在cellForRowAtIndexPath方法中查找的标识符相匹配
  2. 不太可能成为罪魁祸首,但性能调整仍然存在,缓存您拉出的“Tweet”以避免从 NSArray 中获取它的(小)成本
于 2010-03-26T22:11:46.790 回答
0

通过不加载笔尖并完全在代码中创建自定义单元格,您可能会发现很多加速。

否则,您需要进行一些分析以找出任何隐藏的减速潜伏在哪里。

于 2010-03-26T22:01:03.997 回答
0

如果您使用 nib 文件加载表格视图单元格,那么显然加载需要时间并且会产生生涩的感觉。

所以最好的方法是使用代码设计视图并覆盖方法

绘制矩形

在 UITableView 的情况下,这将为您的应用程序提供非常平滑的滚动。

于 2013-05-22T07:22:58.113 回答