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;
}
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.Just note these points..
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...
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.