0

I have this little doubt about reusing UITableViewCell.

When we create UITableViewCell it kinda looks like following.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
      static NSString *CellIdentifier = @"Cell";
      UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
      if (cell == nil) {
          cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault     
                                        reuseIdentifier:CellIdentifier];
          [self configureCell:cell forIndexPath:indexPath];
      }
}

- (void)configureCell:(UITableViewCell *)cell forIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.section) {/**Cell Config Code Goes Here**/}
}

So in my case, every cell in UITableView is different. And if UITableView reuses the cell the cell content is completely different.

Is it good practice to just pass CellIdentifier as nil so every time new cell is created instead given the condition that all cells are different ?

Or should I just move [self configureCell:cell forIndexPath:indexPath]; out and handle it on by my own ?

slonkar
  • 4,055
  • 8
  • 39
  • 63
  • What do you mean by "almost every cell UITableView is different"? Does almost every cell have different subviews, or are you just talking about the content (like label text or image) of the cells? – rdelmar Jul 21 '14 at 05:47
  • @rdelmar: Every cell is different. – slonkar Jul 21 '14 at 05:50
  • You should probably be using static cells if all your cells are different (in which case you wouldn't use cellForRowAtIndexPath at all). – rdelmar Jul 21 '14 at 05:54

3 Answers3

0

I am afraid you have to move the configure cell code out of if condition to make every cell has its own content.

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  
    {
          static NSString *CellIdentifier = @"Cell";  
          UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];  
          if (cell == nil) {  
              cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault      
                                            reuseIdentifier:CellIdentifier];  
          }  
          [self configureCell:cell forIndexPath:indexPath];  
    }

While we saying reuse of UITableViewCells, we mean that we don't have to create the UIView hierarchy inside the cell every time. But you need to configure the content for different cells. Like cell.titleLabel.text = xxxx.

Meanwhile, you can use multiple reuseIdentifiers for different kind of cells. Or if you only have one such cell, you can define a cell as an attribute instance so that you don't have to create it everytime.

CW0007007
  • 5,681
  • 4
  • 26
  • 31
Simon
  • 237
  • 1
  • 7
0

Cell reusability has its sense if you are using cell contents(same subviews) multiple time. Like you have two lables in your tableViewcell for all rows in your tableView. If you have small number of different cells. like if you have three types of cells to use multiple times in your tableView, you can use cell reusability with 3 different cell identifier.

But if you have all different cell, then its fine if you skip cell reusability.

Yogendra
  • 1,728
  • 16
  • 28
0

The proper way of using reusability of the tableView is shown below.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellIdentifier = @"FollowerCell";

UITableViewCell *cell = (UITableViewCell *) [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

if (cell == nil)
{
    cell =[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
[self configureCell:cell forIndexPath:indexPath];

return cell;
}


- (void)configureCell:(UITableViewCell *)cell forIndexPath:(NSIndexPath *)indexPath
{
switch (indexPath.section) {/**Cell Config Code Goes Here**/}
}

The basic idea of reusability is that every time the similar type of cell should not be created, instead they should be reused just by updating their content.

What happens behind the scene is that there is a queue created in which these similar cells are added. Now Suppose there are 200 rows with different data but only 10 rows are visible. That in the queue only approx 14 cells will be present. Now as you will scroll the tableview up or down, this condition UITableViewCell *cell = (UITableViewCell *) [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

checks wether the queue contains any cell, if yes a cell is fetched from the queue. Also the cells, which were earlier visible now on disappearing are added into the queue. This way everytime instead of new cells are created, the already created cells are used.

Now if you forcely make the cell = nil, than every time new cells will be created and added in the queue. Now if there are 200 data than queue will be containing 200 cells thus resulting in increase in memory size.

Hope it will help you in understanding the tableView. Happy Coding :)

Shubhendu
  • 1,081
  • 8
  • 13