简体   繁体   中英

Swift iOS TableView cell items disappears when scrolling

When I scroll through my tableview I see that some badges in my list disappears.

My cell is set to have a custom class of CustomTableViewCell

CustomTableViewCell :

import UIKit

class CustomTableViewCell: UITableViewCell {


    @IBOutlet weak var badgeIcon: AsyncImageView!
    @IBOutlet weak var title: UILabel!

    func configureCell(data: JSON) {

        if let itemType = data["item_type"].int, itemTitle = data["item_type"].string {

          title.text = itemTitle

          if itemType == 1 {
             badgeIcon.hidden = true
          } else {
             badgeIcon.hidden = false
          }
        }

tableview

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

        let cell = tableView.dequeueReusableCellWithIdentifier("myCustomCell") as! CustomTableViewCell

        let data = items[indexPath.row]
        cell.configureCell(data)

        //Dont show highlight
        cell.selectionStyle = UITableViewCellSelectionStyle.None

        return cell

    }

When my tableView first loads everything is displayed right with badgeIcon being shown/hidden where it should be, but if I scroll up/down a few times the badgeIcon will always remain hidden

Within your UITableViewCell subclass, override prepareForReuse with:

badgeIcon.hidden = true

This way you can use a "clean slate" when configuring your cells upon reuse.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM