Swift bool data not passed to tableview cell
03:38 10 Aug 2018

I have a tableview that gets the data from an API. There is a isUnlocked variable that is also getting value from the API. But besides the title and picture, the isUnlocked variable still has the default value set in the custom tableview class.

I pass the value in cellForRow function like this:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    var cell = UITableViewCell()
    if tableView.tag == 1 {
        let c = tableView.dequeueReusableCell(withIdentifier: CourseViewIdentifiers.courses.rawValue, for: indexPath) as! CourseViewTableViewCell
        c.isUnlocked = tableData[indexPath.row].unlocked
        c.selectionStyle = .none
        c.courseTitle.text = tableData[indexPath.row].title
        c.courseImage.loadImageFrom(urlString: tableData[indexPath.row].pic)
        cell = c
    }
    return cell
}

And this is the custom class for tableview cell:

public var isUnlocked: Bool = false

private lazy var backView: UIView = {
    let view = UIView()
    view.backgroundColor = UIColor.lightGray.withAlphaComponent(0.2)
    view.layer.cornerRadius = 5
    view.layer.borderColor = UIColor.lightGray.cgColor
    view.layer.borderWidth = 0.5
    return view
}()

public lazy var courseImage: WebImageloader = {
    let imv = WebImageloader()
    imv.contentMode = .scaleAspectFill
    return imv
}()

public lazy var courseTitle: UILabel = {
    let label = UILabel()
    label.font = UIFont.iransans(size: 18)
    label.textAlignment = .right
    return label
}()

private lazy var courseButton1: UIButton = {
    let button = UIButton(type: .system)
    if !isUnlocked {
        button.setBackgroundImage(#imageLiteral(resourceName: "locked"), for: .normal)
        button.isEnabled = false
    }else{
        button.setBackgroundImage(#imageLiteral(resourceName: "play"), for: .normal)
    }
    return button
}()

private lazy var courseButton2: UIButton = {
    let button = UIButton(type: .system)
    button.setBackgroundImage(#imageLiteral(resourceName: "download"), for: .normal)
    return button
}()

override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
    super.init(style: .default, reuseIdentifier: CourseViewIdentifiers.courses.rawValue)
    print(isUnlocked,"***************")
    setupUI()
    contentView.backgroundColor = .clear
}

required init?(coder aDecoder: NSCoder) {
    fatalError()
}

private func setupUI() {

    backView.translatesAutoresizingMaskIntoConstraints = false
    contentView.addSubview(backView)
    backView.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 15).isActive = true
    backView.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true
    backView.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 5).isActive = true
    backView.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -5).isActive = true
    backView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -5).isActive = true

    courseImage.translatesAutoresizingMaskIntoConstraints = false
    backView.addSubview(courseImage)
    courseImage.rightAnchor.constraint(equalTo: backView.rightAnchor, constant: -10).isActive = true
    courseImage.centerYAnchor.constraint(equalTo: backView.centerYAnchor).isActive = true
    courseImage.heightAnchor.constraint(equalToConstant: 50).isActive = true
    courseImage.widthAnchor.constraint(equalToConstant: 50).isActive = true
    courseImage.layer.cornerRadius = 25
    courseImage.clipsToBounds = true

    courseButton1.translatesAutoresizingMaskIntoConstraints = false
    backView.addSubview(courseButton1)
    courseButton1.leftAnchor.constraint(equalTo: backView.leftAnchor, constant: 10).isActive = true
    courseButton1.centerYAnchor.constraint(equalTo: backView.centerYAnchor).isActive = true
    courseButton1.heightAnchor.constraint(equalToConstant: 30).isActive = true
    courseButton1.widthAnchor.constraint(equalToConstant: 30).isActive = true
    courseButton1.layer.cornerRadius = 15
    courseButton1.clipsToBounds = true

    if isUnlocked {
        courseButton2.translatesAutoresizingMaskIntoConstraints = false
        backView.addSubview(courseButton2)
        courseButton2.leftAnchor.constraint(equalTo: courseButton1.rightAnchor, constant: 10).isActive = true
        courseButton2.centerYAnchor.constraint(equalTo: backView.centerYAnchor).isActive = true
        courseButton2.heightAnchor.constraint(equalToConstant: 30).isActive = true
        courseButton2.widthAnchor.constraint(equalToConstant: 30).isActive = true
        courseButton2.layer.cornerRadius = 15
        courseButton2.clipsToBounds = true
    }

    courseTitle.translatesAutoresizingMaskIntoConstraints = false
    backView.addSubview(courseTitle)
    courseTitle.rightAnchor.constraint(equalTo: courseImage.leftAnchor, constant: -10).isActive = true
    courseTitle.topAnchor.constraint(equalTo: courseImage.topAnchor).isActive = true
    courseTitle.leftAnchor.constraint(equalTo: courseButton1.rightAnchor, constant: 10)

}

The isUnlocked variable has the default False value no matter what I set it in the cellForRow function. Guys it's killing me please help.

ios swift