我有一个从API获取数据的表格视图。有一个 isUnlocked 变量,该变量也从API中获取价值。
但是,除了标题和图片之外, isUnlocked 变量仍具有在自定义tableview类中设置的默认值。
我像这样在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
}
这是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)
}
无论我在cellForRow函数中设置了什么,isUnlocked变量均具有默认的False值。伙计们,这杀了我,请帮忙。
最佳答案
我认为正在设置您的值,但是您的UI并未更新,因为它仅在初始化期间设置,显然Bool
的值将始终为默认值(false)。因此,与其在setUpUI()
中调用init()
,不如在出队后调用它。
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
}
或至少将
courseButton2
的设置移至另一个方法并调用该方法。