[ACCEPTED]-Disable cell reuse for small fixed-size UITableView-uitableview

Accepted answer
Score: 19

Set nil for reuse identifier in the line

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];

Or 1 just remove the line and add,

UITableViewCell *cell = nil;
Score: 7

Just do not implement the method UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SomeID"]; and none 3 of your cells will be reused. Each time 2 it ask for a cell you create a new one and 1 configure it.

Score: 6

You should pass nil in the method initWithStyle:reuseIdentifier: if you don't 3 want to reuse cells but keep in mind the 2 performance. As long as it is good, you 1 should be ok passing nil.

Score: 6

First three answers are completely correct, you 15 just need to make sure that you do not call 14 dequeueReusableCellWithIdentifier function on a UITableView. In addition to this, you 13 can simply allocate the cells in code. First 12 you will need an instance variable that 11 will store the pointers to your cells (I 10 will assume you use a normal view controller):

@interface MyViewController

@property (nonatomic, strong) NSArray* myTableViewCells;


Then 9 you can lazily instantiate this array, by 8 overriding it's getter:

- (NSArray *)myTableViewCells
    if (!_myTableViewCells)
        _myTableViewCells = @[ 
                                [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil],
                                [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil]

    return _myTableViewCells;

Add more cells into 7 array if you like, or use NSMutableArray. Now all you 6 have to do is to wire up this array to proper 5 UITableViewDataSource methods.

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    return self.myTableViewCells.count;

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    UITableViewCell* cell = self.myTableViewCells[indexPath.row];

    // Add your own modifications

    return cell;

This makes for a much cleaner code, less 4 prone to memory leaks (static variables get deallocated when program ends, so why are we keeping table view cells in memory if the view controller that is displaying them is already gone?).

The addition of 3 new cells is also much easier (no switch 2 or if statements required) and code is more 1 nicely structured.

Score: 2


Sometimes you need some cells to be 16 static, for example, you need the first 15 cell to be downloading cell which has download 14 progress bar. and other cells to be waiting 13 for download cells. In this case, the first 12 cell should be accessible for pause and 11 resume functions(outside tableView:cellForRowAtIndexPath:).

you 10 could try to create static cells like this:

1st: subclass 9 UITableViewCell, to create your own cell 8 (this is a option)

2nd: crate static cell 7 in your view controller

static YourSubclassedTableViewCell *yourCell_0;
static YourSubclassedTableViewCell *yourCell_1;
static YourSubclassedTableViewCell *yourCell_2;
static YourSubclassedTableViewCell *yourCell_3;

3rd: Init cells in 6 viewDidLoad (viewDidLoad is a good choice 5 to put init code)

- (void)viewDidLoad
    yourCell_0 = [[YourSubclassedTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
    yourCell_1 = [[YourSubclassedTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
    yourCell_2 = [[YourSubclassedTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
    yourCell_3 = [[YourSubclassedTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];

    // or simply
    yourCell_0 = [[YourSubclassedTableViewCell alloc] init];
    yourCell_1 = [[YourSubclassedTableViewCell alloc] init];
    yourCell_2 = [[YourSubclassedTableViewCell alloc] init];
    yourCell_3 = [[YourSubclassedTableViewCell alloc] init];


4th: Load cell

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    switch (indexPath.row) {
        case 0:
            yourCell_0.textLabel.text = @"1st Row";
            return yourCell_0;

        case 1:
            yourCell_1.textLabel.text = @"2nd Row";
            return yourCell_1;

        case 2:
            yourCell_2.textLabel.text = @"3rd Row";
            return yourCell_2;

        case 3:
            yourCell_3.textLabel.text = @"4th Row";
            return yourCell_3;

            return defaultCell;

**As described 4 above, cells are created once and can be 3 accessed outside tableView:cellForRowAtIndexPath:

You 2 also could declare cells as @property to 1 make it accessible for other class.

Score: 2

Just alloc a new cell in place of dequeue 3 no need to do any of the above. Performance 2 implications are negligible for small tableView 1 (< 100 cells).

Example in swift 3

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = UITableViewCell(style: .default, reuseIdentifier:"Cell")
    return cell


More Related questions