简体   繁体   English

Swift bool数据未传递到TableView单元格

[英]Swift bool data not passed to tableview cell

I have a tableview that gets the data from an API. 我有一个从API获取数据的表格视图。 There is a isUnlocked variable that is also getting value from the API. 有一个isUnlocked变量也可以从API获取价值。 But besides the title and picture, the isUnlocked variable still has the default value set in the custom tableview class. 但是,除了标题和图片之外, isUnlocked变量仍具有在自定义tableview类中设置的默认值。

I pass the value in cellForRow function like this: 我像这样在cellForRow函数中传递值:

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: 这是tableview单元的自定义类:

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. 无论我在cellForRow函数中设置了什么,isUnlocked变量均具有默认的False值。 Guys it's killing me please help. 伙计们正在杀了我,请帮忙。

I think your value is being set, but your UI is not being updated because it only gets set during initialization, where obviously the value of the Bool will always be the default value (false). 我认为正在设置您的值,但是您的UI并未更新,因为它仅在初始化期间设置,显然Bool的值将始终为默认值(false)。 So instead of calling setUpUI() in init() , call it after dequeue-ing. 因此, setUpUI()init()中调用setUpUI()setUpUI()在出队后调用它。

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)
        c.setUpUI()
        //print(c.isUnlocked)
        cell = c
    }
    return cell
}

Or atleast move the set up of courseButton2 to another method and call that method. 或者至少将courseButton2的设置移至另一个方法,然后调用该方法。

Problem solved by adding this block of code: 通过添加以下代码块解决了问题:

DispatchQueue.main.async {
        if DownloadManager.isDownloading(url: self.course_url!) {
            self.courseButton2.alpha = 0.5
            self.courseButton2.isEnabled = false
        }
    }

Just add this code to the cell, and your ok to go! 只需将此代码添加到单元格中,就可以了!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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