0

我有一个 UITableView,它正在从 MPMediaQuery 加载专辑列表。一切工作正常,直到我注意到一张带有非方形专辑封面的专辑将内置单元格视图的标签文本推向右侧。注意到这是我的失败,因为在此过程中我还注意到滚动不太流畅。

我用笔尖做了一个单元,解决了定位问题,但性能仍然受到影响。在这一点上,我开始做大量的研究,并尽我所能去尝试。

  • 首先是来自 nib 的单元格,带有一个 imageview 和两个标签
  • 为了纠正 imageview 中奇怪的缩放比例,我在 uiview 中添加了 imageview 来裁剪它(对性能没有真正的影响)。
  • 加载图像没有任何后处理,仍然抖动性能。
  • 在 -(void)drawRect 中实现单元格(如 Apple 的 TableViewSuite 示例 #5 中的示例),从 tableview 传递图像。这最初只加载了前约 8 张图像,性能只是略微卡顿。
  • 然后我设置 [myCell setNeedsDisplay] 让它重新绘制单元格,以便所有其他图像都能正确显示,性能又下降了。我还尝试只用图像重绘部分而不做任何更改。
  • 这种只有一张静态图像的方法产生了完美的性能。

我确保所有正常的东西,如 cellIdentifier 等都是正确的,如果单元格视图只有文本,它可以完美滚动。

下面找到使用默认单元格视图之一的 tableviewcell 的实现,以说明我当前如何为单元格获取图像:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Cell initizalation
static NSString *CellIdent = @"CellIdent";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdent];
if (cell == nil) {
    cell = [[UITableViewCell alloc]
            initWithStyle:UITableViewCellStyleSubtitle
            reuseIdentifier:CellIdent];
}

cell.imageView.image = [[[[albumList
                       objectAtIndex:indexPath.section]objectAtIndex:indexPath.row] valueForProperty:MPMediaItemPropertyArtwork]imageWithSize:CGSizeMake(0,56)];
//it seems to scale with a sharper image if CGSizeMake has a value, rather than 
//CGSizeZero. It doesn't actually do any scaling at this point, which I initially thought
//it would, but the results were more what I wanted so...

return cell;


}

在 Apple 的 ipod 应用程序中,带有艺术卷轴的专辑列表就像丝绸一样,很明显,我错过了一些东西。

4

1 回答 1

0

这个答案将有所帮助:

提高 iPhone UITableView 滚动性能的技巧?

尤其是第 3 步:

在 UITableViewCell 的 drawRect 中绘制所有内容:如果可能,不惜一切代价避免子视图(或者,如果您需要标准的可访问性功能,内容视图的 drawRect:)

例子 :

- (void)drawRect:(CGRect)rect {
   [yourImage drawAtPoint:CGPointMake(0,0)];
}

如果您自己绘制图像,您可以找到 Apple 的示例应用程序来说明性能提升:

https://developer.apple.com/library/ios/#samplecode/AdvancedTableViewCells/Introduction/Intro.html#//apple_ref/doc/uid/DTS40009111

如果您想要完整而清晰的解释,请查看此 WWDC 会议,其中详细介绍了 UITableViewPerformance:

https://developer.apple.com/videos/wwdc/2010/?id=128

于 2012-03-12T20:39:33.923 回答