views:

285

answers:

3

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;
}
A: 

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.

Shaggy Frog
Drive-by negs suck.
Shaggy Frog
+1  A: 

Couple things:

  1. Make sure the cell's identifier in IB matches what you're looking for in the cellForRowAtIndexPath method
  2. 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
Nick Veys
+2  A: 

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).

FRotthowe
Note that this implementation ("drawing directly") requires you to think carefully about accessibility features, which you get for free by using UILabels.
Alfons
I didn't have the cell identifier set to TwitterCell in IB so that gave me a noticeable improvement... but I think this implementation is definitely the best way to get optimal performance. Thank you!
Charles S.