1

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];
    }
}
NSPratik
  • 4,714
  • 7
  • 51
  • 81
  • 4
    Is there any particular reason why you don't want to reuse the table cells? – Ilanchezhian May 04 '12 at 10:13
  • Reuse will show content of the disappearing cell whose memory the current cell uses... – NSPratik May 04 '12 at 10:16
  • 1
    But after dequeueing the reused cell, you can reconfigure it with the correct content you want to display. The problem with not reusing cells, is that, if the user Table View is long enough and the user scrolls fast throuhg it, will cause a lot of allocs / deallocs, which will result on a not smooth scrolling. – flainez May 04 '12 at 10:24

2 Answers2

2

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.

lawicko
  • 7,246
  • 3
  • 37
  • 49
  • 2
    I think it is worth noting that this code is valid only for projects that are using ARC. Otherwise, you will be leaking cells. The solution for projects not using ARC is to autorelease the cell before returning it. – flainez May 04 '12 at 10:28
  • 1
    Good point, I tend to forget that not everyone is so lucky to be able to use ARC. – lawicko May 04 '12 at 11:47
1

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.

Adem Özgür
  • 200
  • 1
  • 10