Davis
Davis

Reputation: 1263

Bad Perfomance with TableView and Autolayout

i have 20 different Custom UITableViewCells (and its getting more), and all of them differ from each other extremely (different Content, different Height...).

I´m getting all the Datasource from my Server so it takes a while until the Content is available in my UITableView.

I build all my Custom Cells in Code and with Autolayout.

When i open now my UITableView and i start to scroll, the UITableView stuck for a moment when it comes to visibility and i´m not sure how to solve this Problem.

Here are some of the important Code Snippets:

- (void)viewDidLoad 
{

[super viewDidLoad];

self.tableView.estimatedRowHeight = 250.0f;
self.tableView.rowHeight = UITableViewAutomaticDimension;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
GTBlockCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

//Here i save my data from the Server into my Model(GFBlock)
GFBlock *block = [self.node.blocks objectAtIndex:indexPath.row];

//I think this part, is the reason why it stucks sometimes, here i call a method which build each of my Custom Cells
cell = [[GTAppController sharedInstance]  createInterfaceForBlock:block];

[cell setNeedsUpdateConstraints];
[cell updateConstraintsIfNeeded];

return cell;
}


//Create the Interface for each Cell
- (GTBlockCell *)createInterfaceForBlock:(GFBlock *)block 
{

NSString* className = [self.interfacesDict objectForKey:NSStringFromBlockType(block.blockTypeId)];

Class c = NSClassFromString(className);


//Open the specific Custom Cell
GTBlockCell* instance = [(GTBlockCell *)[c alloc] initWithBlock:block];

return instance;
}

I should also mention that some of my Custom Cells are having extremly various row heights. I also tried to play with estimatedRowHeight but nothing really changed.

Maybe some of you have some other good ideas?

Upvotes: 0

Views: 69

Answers (1)

jrturton
jrturton

Reputation: 119242

First, use instruments to find out where your code is really spending its time instead of guessing that autolayout is to blame.

Second, you're not reusing your cells. Scrolling performance depends heavily on cell reuse.

GTBlockCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

This line dequeues a cell

cell = [[GTAppController sharedInstance]  createInterfaceForBlock:block];

This line throws that away and builds a brand new one.

You need to separate populating your cells with creating them. If you have several different types of cell then use a reuse identifier for each one so that you dequeue the appropriate type of cell, then populate it with the right data. Register each table cell subclass with the tableview before you start, then you will always get either a new cell or a recycled cell back from the table.

Upvotes: 1

Related Questions