我有下面的代码,它只创建了一个自定义的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;
}
- 动态行高是昂贵的,因为它无法有效地缓存渲染的视图,因为运行时在调用之前不知道给定单元格的返回高度。如果可能的话,把它扔掉。苹果工程师告诉我,把所有单元格都画得比需要的高一点,以容纳几行更大的单元格,比使用动态高度更有效
- 缓存
[items objectAtIndex:indexPath.row]
返回的对象 - 我对你的cell.avatarImageView了解不多,但如果它没有根据URL对图像进行缓存,那么每次显示单元格时,它都会调用互联网或文件系统来重新加载该图像。试试EGOImageView堆栈,它可以有效地缓存图像,并且是一些非常流畅的代码
- 当您在EGO github代码中时,获取它们的EGOCache并使用它来缓存您必须解析的一些其他值,例如bodyLabel文本
- 如果您对该单元格的任何看法都是透明的,请观看WWDC 2011关于UIKit性能的视频。他们有一个更有效的方法来在表视图单元格上绘制透明度
- 为什么要动态更改标签的位置-调用[Utils alignLabelWithTop:]
还观看了WWDC关于使用仪器的视频,他们介绍了如何找到绘制代码会扼杀性能的地方。今年有一些(有些,不是全部)非常棒的会议。