eol
eol

Reputation: 23

iOS Swift dequeueReusableCellWithIdentifier causing overlap

This has been driving me mad, I have look at about every post on around in regard to this.

I am using custom tableview cell classes and XIB files. It all works fine, until I scroll the table view off screen (that old chestnut). I know it has to do with the dequeueReusableCellWithIdentifier method and not clearing the old data. However I have no idea how to implement this.

Once the cell scrolls off screen., it causes an overlap and continues to add images to the cell.

let cell: DetailHeaderTableViewCell = tableView.dequeueReusableCellWithIdentifier("detailHeaderTableViewCell") as DetailHeaderTableViewCell

From what I have found, I need to implement something like this:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];

UIImageView *defaultImageView;
UILabel *customLabel;

if (cell == nil) {
    // create the cell and empty views ready to take the content.
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
} else { ... }

However I cannot work out how to achieve this in swift.

Many thanks in advance.

Upvotes: 1

Views: 1813

Answers (1)

Michael Dautermann
Michael Dautermann

Reputation: 89509

What you're looking for is UITableViewCell's prepareForReuse method.

If you're using custom UITableView cells, you need to implment this method to clear out any old data from your cells before they are reused.

Upvotes: 1

Related Questions