优化自定义UITableViewCell创建

时间:2011-06-25 01:29:51

标签: iphone objective-c ios uitableview

我有以下代码,只是创建一个自定义UITableViewCell。我正在创建一个动态行高,那贵吗?有什么方法可以优化吗?

我也在调整cellForRow中某个标签的框架。有没有办法优化它?

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MessageCell *cell = (MessageCell*)[self tableView:tableView cellForRowAtIndexPath:indexPath];
    return cell.bodyLabel.bounds.size.height + 30;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"MessageCell";

    MessageCell *cell = (MessageCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[MessageCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    cell.usernameLabel.text = [[items objectAtIndex:indexPath.row]valueForKey:@"user_login"];
    cell.bodyLabel.text = [[[items objectAtIndex:indexPath.row]valueForKey:@"body"]gtm_stringByUnescapingFromHTML];
    [Utils alignLabelWithTop:cell.bodyLabel];
    cell.dateLabel.text = [Utils toShortTimeIntervalStringFromStockTwits:[[items objectAtIndex:indexPath.row]valueForKey:@"created_at"]]; 
    [cell.avatarImageView reloadWithUrl:[[items objectAtIndex:indexPath.row]valueForKey:@"avatar_url"]];

    return cell;
}

1 个答案:

答案 0 :(得分:8)

  • 动态行高是昂贵的,因为它无法有效地缓存渲染的视图,因为运行时不知道在进行调用之前您将为给定单元返回的高度。如果可能的话,摆脱它。 Apple工程师告诉我,除了使用动态高度之外,将所有单元格绘制得比需要更高一些的单元更高效,而不是使用动态高度。
  • 缓存[items objectAtIndex:indexPath.row]
  • 返回的对象
  • 我对你的cell.avatarImageView了解不多,但如果没有基于URL对图像进行一些缓存,那么每次调用时都会调用互联网或文件系统重新加载该图像显示单元格。试试EGOImageView stack,它可以有效地缓存它的图像,并且是一些非常漂亮的代码。
  • 当您使用EGO github代码时,抓住他们的EGOCache并使用它来缓存您必须解析的其他一些值,例如bodyLabel文本
  • 如果您对该单元格的任何观看是透明的,请观看有关UIKit性能的WWDC 2011视频。他们有一个更有效的方法来绘制tableview单元格的透明度
  • 为什么要动态更改标签的位置 - 调用[Utils alignLabelWithTop:]?

同时观看使用乐器的WWDC视频,他们会查看如何找到绘图代码杀死性能的位置。今年有一些(有些,不是全部)非常棒的会议。

相关问题