Best practice in UITableView with different kind of UITableViewCell

1.3k views Asked by At

her's what we need to do:

  • the data source of the tableView core data.
  • a tableView that contain 5 different type of custom tableViewCell.
  • every tableViewCell is completely different from others.
  • some tableViewCell will have a progressBar.

we have 3 solutions:

  1. use a unique tableViewCell with a unique reuse identifier.
  2. use a unique tableViewCell with 5 reuse identifier.
  3. use 5 tableViewCell and 5 reuse identifier.

we test the 1st solution, it's ok in iphone 5/ iphone 4S, but in iphone 4 it's slooooooow (and we need to support the 3GS too ...).

The question: which solution will be better? or if you have other solution it will be great.

the favor: can you explain how the reuse identifier work (in details please :) ), like when the first cells are allocated, when they are reused (with 1 and with different reuse identifier), when they are desallocated ... ?

thank you.

2

There are 2 answers

0
Calin Chitu On

This was my solution, and it works ok on 3gs, now depends how complex is your cell and how many things you do @ [cell load] method. Try to avoid for/while loops there.

    if(indexPath.row == 0){

        static NSString *CellIdentifier = @"HeadCell";
        kHeaderCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        [cell load];
        return cell;
    }
    if(indexPath.row == 1){
         static NSString *CellIdentifier = @"HistCell";
         kHisoryCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        [cell load];
        return cell;
    }

...and so on

0
RTasche On

If all cells are completely different (layout) you should init each cell type with a unique reuse identifier to benefit from the performance advantages of dequeing cells later. The tableview will init as many cells as necessary (depending on the number of sections and rows in each section) for filling its bounds no matter which reuse identifier you assign. Cells are cached as they disappear from the visible area of the tableview. That means at least one cell of each reuse identifier that has gone off the screen is kept in memory for reuse. When the tableview is scrolled and another row is needed it will ask cellForRowAtIndexPath to provide a cell for this row. When there's no cell with a specified resuse identifier in the queue a new cell is created, subviews are initialized and layout / arrangement of subviews is done. If there's a cell with the specified resuse identifier in cache the tableview takes this cell "as is" and customizes it just according to the specifications you provide in cellForRowAtIndexPath like assign a different image to an imageView or set a label's text which is much more "inexpensive" than creating a completely new cell. You can check that by only setting a label's text in your custom cell's initWithStyle. If you don't modify the text after calling dequeueReusableCellWithIdentifier in cellForRowAtIndexPath the label's text will be the same in every cell dequeued with the same reuse identifier. Also complex backgrounds (e.g. with gradients) will be reused and don't need to be redrawn everytime a new cell appears on the screen. Assigning ONE reuse identifier to all different types of cells, reusing the cells would cause nearly the same effort as creating a new cell for each row (assuming each cell type equally spread). Cells in the queue will be deallocated when the tableview is deallocated. Hope this helps you understanding the concept of reusing tableview cells.