Question

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;
}
Was it helpful?

Solution

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

OTHER TIPS

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

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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top