NSPratik
NSPratik

Reputation: 4858

UItableviewcell "cell-identifier" memory management

If we give same Identifier to all cells, Disappearing cell uses the memory of Appearing cell. Means content will repeat when I scroll Table-view. But If we give diff Identifier then every cell will have its own memory location and shows data perfectly.

Now suppose I have 1000 or more records to load in Table-view. If I will give different Identifiers, there will be lots of allocations in memory. So Is there any solution to show data perfectly with minimum memory allocation ?

Here is how I define cell identifier:

-(UITableViewCell *)tableView:(UITableView *)tableView 
        cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellIdentifier = [NSString stringWithFormat:@"%d%d",indexPath.section,indexPath.row];
    UITableViewCell *Cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (Cell == nil) 
    {
        Cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle 
                                      reuseIdentifier:cellIdentifier];
    }
}

Upvotes: 1

Views: 1461

Answers (2)

Adem Özgür
Adem Özgür

Reputation: 201

you should clear the contents of dequeued cell like emptying labels and others. if you allocate separate memory for each cell you will easily go low memory. perfect memory management is still reusing cells.

Upvotes: 1

lawicko
lawicko

Reputation: 7344

The problems that you are experiencing are caused by you improperly using cell identifiers. Cell identifier should be the same for all cells that you want to reuse. Take a look at this template, it should explain the correct approach:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *cellIdentifier = @"MY_CELL";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
        // everything that is similar in all cells should be defined here
        // like background colors, label colors, indentation etc.
    }
    // everything that is row specific should go here
    // like label text, progress view progress etc.
    return cell;
}

Btw. use camel case to name your variables, capitalized names are meant for class names.

Upvotes: 2

Related Questions