Jaafar Bk Jaafar Bk - 1 year ago 81
iOS Question

how to avoid reusable custom cell from overwriting data when scrolling?

This is the code I am using and it works fine until I scroll in the table view, the cells overwrite each others and all the repostedFromLabel are hidden.

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

let cell = tableView.dequeueReusableCellWithIdentifier("FeedsTableViewCell", forIndexPath: indexPath) as? FeedsTableViewCell
if (arrayofPostsFeed[indexPath.row].postedByUser!.nickName!) != arrayofPostsFeed[indexPath.row].ownedByUser!.nickName! {
cell!.repostedFromLabel.text! = "Reposted From \(self.arrayofPostsFeed[indexPath.row].postedByUser!.nickName!)"
}else {
cell!.repostedFromLabel.hidden = true


how should I prevent reusable cells to override each others?

Answer Source

You can implement the prepareForReuse function in your FeedsTableViewCell to reset the hidden property of the repostedFromLabel. Something like this:

override func prepareForReuse() {
    self.repostedFromLabel.hidden = false

At the moment you are not resetting that value, so it will get mixed up when reusing the cells.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download