How can I speed up a UITableView?

2019-01-30 02:30发布

I have a UITableView with about 400 cells in 200 sections and it's a little sluggish in responding to user interaction (scrolling, selecting cells.) I've made sure the methods for retrieving cells and header views do the bare minimum as it's running, and I don't think I'm doing anything out of the ordinary to make it slow. The cells and headers just have a background image and text. Has anyone else had this kind of problem, and do you know any way to make it run a little faster?

Edit: I'm offering a bounty because I'd love to get some useful feedback on this. I don't think the answer lies in a problem in my code. Instead I'm looking for strategies to re-engineer the UITableView so that it runs faster. I'm totally open to adding new code and I look forward to hearing what you guys have to say.

Sluggishness is observed on both the simulator and my device, an iPhone 4. Here are my implementations of viewForHeaderInSection and cellForRowAtIndexPath, which are the only UITableViewDelegate methods implemented nontrivially. I am reusing cells and header views.

- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger) section
{
    HaikuHeaderView* view= [m_sectionViews objectAtIndex:section];
    NSMutableArray* array= [m_haikuSearch objectAtIndex:section];
    Haiku* haiku= [array objectAtIndex:0];

    [view.poetLabel setText:[haiku nameForDisplay]];

    return view;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];

        cell.backgroundView= [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"cell gradient2.png"]];

        // (Set up a bunch of label attributes in the cell...)
    }

    NSMutableArray* array= [m_haikuSearch objectAtIndex:indexPath.section];
    Haiku* haiku = [array objectAtIndex:indexPath.row];
    cell.textLabel.text = [haiku.m_lines objectAtIndex:0];

    return cell;
}

9条回答
兄弟一词,经得起流年.
2楼-- · 2019-01-30 03:10

Are you using a lot of subviews?

If so, a good technique is to, instead of adding a lot of labels and images, draw them using CoreGraphics.

To do this, you'd have to subclass UITableViewCell and implement the -(void)drawRect:(CGRect)rect method.

查看更多
虎瘦雄心在
3楼-- · 2019-01-30 03:14

Just note these points..

  1. Are you reusing the cells..Which is a good practice to do..
  2. Make sure you are not doing any expensive calculations in cellForRowAtIndexPath callback, or in a function called from CellForRowAtIndexPath..
  3. You said there is a background image. Another reason that you must reuse your cell.

Some good info about cell reuse is here..

EDIT : Found this page very late..

This SO question thread might help you...especially the accepted answer...

查看更多
爷的心禁止访问
4楼-- · 2019-01-30 03:22

Two suggestions: One is to use -initWithStyle:reuseIdentifier: for your table view cells instead of -initWithFrame:. The other is to comment out setting the cell.backgroundView to an image with a gradient and see if that's the culprit. Every time I've had poor performance in a table view it's been because of an image.

查看更多
登录 后发表回答