-2

I have a tableView with three sections. Initially there was a issue of cell content duplication.

It was solved by setting the reuse identifier of the cell to nil. After that, a black bar/color occurred at the position of first cell in the 3rd section, like on this screen shots.

This was my previous issue.And changed the cell implementation to,

static NSString *cellIdentifier=[NSString stringWithFormat:@"Cell%ld%ld",(long)indexPath.section,(long)indexPath.row];

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

if(cell==nil){

    cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];


}

Does anyone know what's the reason? Any help appreciated.

Raghee M
  • 11
  • 4

2 Answers2

1

At first, the most likely reason for cell content duplication is that cell's subviews are not reused but created every time cell is configured and added to the view hierarchy. The old subviews are not removed, hence the duplicate content.

To fix it, create all the subviews needed during cell initialization stage. During cell configuration only update those views with content, change their size, but do not create new views or remove old.

Reset cell to the default appearance by overriding and implementing prepareForReuse() PrepareForReuse method description

At last, setting reuse identifier to nil is a bad practice and can lead to an undefined behavior (as shown on the screenshots). Cell's reuse identifier is set during its initialization and should not be changed during cell lifecycle.

Richard Topchii
  • 7,075
  • 8
  • 48
  • 115
  • Cell's reuse identifier are not set during initialisation. The cell has got nothing to do with the identifier, the `tableView` is the one using the id. However, setting the reuse identifier to `nil` is bad practice, but not because it leads to unexpected behaviour, we know what will happen exactly. The `tableView` will not be reusing the cells, it will create a new `UITableViewCell` instance every time it displays a cell. If you have a large number of cells, this could lead to performance issues, e.g. scrolling in the tableView will be super slow. – dirtydanee Jan 03 '17 at 12:33
  • @richard topchiy sorry its not worked for me. can u plz share the code? – Raghee M Jan 03 '17 at 12:54
  • @dirtydanee, In the link I've attached here is the description of UITableViewCell designated initializer. It requires reuseIdentifier or nil. As far as I know, there is no way to change reuse identifier after the cell has been created, therefore the reuse identifier must be supplied at the initialization stage. https://developer.apple.com/reference/uikit/uitableviewcell/1623276-init – Richard Topchii Jan 03 '17 at 14:34
0

Looking at your code it seems like you are creating a new identifier for each cell.

[NSString stringWithFormat:@"Cell%ld%ld",(long)indexPath.section,(long)indexPath.row]

This line will create a new identifier for each row, like Cell00, Cell01, etc..

However, it should not be dynamic, it should be, like prefix of the variable suggests, static.

static NSString *cellIdentifier= @"Cell"; // or whatever is it in IB or registered by
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];;

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

Also, the duplication could occur, when you forget to implement prepareForReuse(). This is the function, what will be called by the OS, when your cell is being reused. You can set all values to nil, so you avoid the duplication.

EXAMPLE:

- (void)prepareForReuse {
    [super prepareForReuse];
    // Then Reset here back to default values that you want.
    self.titleLabel.text = nil;
    self.imageView.image = nil; 
}
Community
  • 1
  • 1
dirtydanee
  • 6,081
  • 2
  • 27
  • 43