gpt4 book ai didi

ios - 如何在选择自定义 TableView 时更改单元格 UI 元素的状态?

转载 作者:行者123 更新时间:2023-11-29 05:12:40 25 4
gpt4 key购买 nike

我的表格 View 如下所示

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return appsName!.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TableViewCell
cell.appTimeLabel.isHidden = array[indexPath.row]
cell.appNameLabel.text = appsName![indexPath.row]
cell.appTimeLabel.text = appTimeLimit![indexPath.row]
return cell
}

我的 TableView 单元格类:

class TableViewCell: UITableViewCell {

@IBOutlet weak var appNameLabel: UILabel!
@IBOutlet weak var setLimitButton: UIButton!
@IBOutlet weak var appIcon: UIImageView!
@IBOutlet weak var appTimeLabel: UILabel!

override func awakeFromNib() {
super.awakeFromNib()
}

override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}

}

我的行选择功能:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
array[indexPath.row] = !array[indexPath.row]
}

bool 值数组:

var array = [Bool](repeating:false, count: 200)

这是我的代码,我想显示无限标签并在单元格中对其选择设置限制按钮:

enter image description here

最初设置限制按钮和无限标签设置为隐藏。

实际工作单元截图(android):

enter image description here enter image description here

如何做到这一点?

最佳答案

您可以为单元格数据创建一个字典,如下所示;

  var myDic = [["appName": "app1",
"appTime": "unlimited",
"isSelected": true,
], ["appName": "app2",
"appTime": "unlimited",
"isSelected": true,
]
]

之后你可以像这样使用它的计数;

 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return myDic.count
}

如果您根据字典中的“isSelected”值更改复选框状态,您的问题就会解决;

 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {



let cell = myTableView.cellForRow(at: indexPath) as! TableViewCell

if (myDic[indexPath.row]["isSelected"] as? Bool) == true {
myDic[indexPath.row]["isSelected"] = false
} else {
myDic[indexPath.row]["isSelected"] = true
}
cell.appTimeLabel.isHidden = !(myDic[indexPath.row]["isSelected"] != nil)
cell.setLimitButton.isHidden = !(myDic[indexPath.row]["isSelected"] != nil)
cell.selectionBox.isSelected = (myDic[indexPath.row]["isSelected"] != nil)

myTableView.reloadData()

}

关于ios - 如何在选择自定义 TableView 时更改单元格 UI 元素的状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59533580/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com