问题
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;
}
回答1:
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).
回答2:
Couple things:
- Make sure the cell's identifier in IB matches what you're looking for in the
cellForRowAtIndexPath
method - Unlikely to be the culprit, but a performance tweak nontheless, cache the "Tweet" that you pull out to avoid the (small) cost of grabbing that out of the NSArray
回答3:
You'll probably find a lot of speedup by not loading nibs and creating your custom cells completely in code instead.
Otherwise you'll need to do some profiling to figure out where any hidden slowdowns are lurking.
回答4:
If you are using a nib file to load the table view cell then obviously it will take time to load and will give a jerky feeling.
So the best way to do so is to design the view using codes and override the method
drawInRect
This will give your application a very smooth scroll in case of UITableView.
来源:https://stackoverflow.com/questions/2526787/iphone-uitableview-stutters-with-custom-cells-how-can-i-get-it-to-scroll-smooth