iPhone UITableView结合了自定义单元格。如何让它顺利滚动? [英] iPhone UITableView stutters with custom cells. How can I get it to scroll smoothly?
问题描述
我正在使用UITableView显示使用Interface Builder创建的自定义单元格。表滚动不是很平滑(它断断续续)让我相信单元格没有被重用。这段代码有什么问题吗?
I'm using a UITableView to display custom cells created with Interface Builder. The table scrolling isn't very smooth (it "stutters") which leaves me to believe cells aren't being reused. Is there something wrong with this code?
- (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;
}
推荐答案
也许来自atebits的博客条目(Tweetie的创建者)可以帮助您: http:/ /blog.atebits.com/2008/12/fast-scrolling-in-tweetie-with-uitableview/
Maybe this blog entry from atebits (creators of Tweetie) can help you: http://blog.atebits.com/2008/12/fast-scrolling-in-tweetie-with-uitableview/
切割追逐,这是秘密:每个表格单元一个自定义视图,并做自己的绘图。听起来很简单?那是因为它是。它实际上比处理标签和图像的大量子视图更简单,并且它的速度提高了大约数十亿倍(根据我的非正式测试)。
Cutting to the chase, here’s the secret: One custom view per table cell, and do your own drawing. Sounds simple? That’s because it is. It’s actually simpler than dealing with a ton of subviews of labels and images, and it’s about a bzillion times faster (according to my informal tests).
这篇关于iPhone UITableView结合了自定义单元格。如何让它顺利滚动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!