iPhone UITableView stutters with custom cells. How

2019-07-19 03:53发布

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

4条回答
混吃等死
2楼-- · 2019-07-19 04:16

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
查看更多
劳资没心,怎么记你
3楼-- · 2019-07-19 04:18

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楼-- · 2019-07-19 04:20

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.

查看更多
Anthone
5楼-- · 2019-07-19 04:26

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

查看更多
登录 后发表回答