gpt4 book ai didi

ios - 双击 UITableView 单元格应该转到以前的状态

转载 作者:行者123 更新时间:2023-11-28 15:35:48 24 4
gpt4 key购买 nike

如果我选择(单击)TableView 的行,它应该添加图像说明我选择了这个特定的项目。一切正常!

我的问题是:如果用户想从该选定项目返回。如果我点击同一行,它应该取消选择该单元格并隐藏该图像。

我尝试的是:

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

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath as IndexPath) as! leistungCell

// Configure the cell...
let tableData = self.tableData[indexPath.row]

cell.leistungLbl.text = tableData["leistung_info"] as? String

//space between Rows
cell.contentView.backgroundColor = colorLightGray
cell.contentView.layer.borderColor = UIColor.white.cgColor


//space between Rows
cell.contentView.layer.borderWidth = 3.0
cell.contentView.layer.cornerRadius = 8

return cell


}

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


let cell = tableView.cellForRow(at: indexPath)


cell?.imageView?.image = UIImage(named: "check.png")

let value = Info["leistung_info"] as! String

}

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

let cell = tableView.cellForRow(at: indexPath)
cell?.imageView?.image = nil
}

最佳答案

忘记并删除 didDeSelectRowAt,只需使用 didSelectRowAt 和一个数组来保存选择:

var selectedIndexPaths = [IndexPath]()

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath)

if let index = selectedIndexPaths.index(of: indexPath) { //deselect it if the row is selected
tableView.deselectRow(at: indexPath, animated: true)
cell?.imageView?.image = nil
selectedIndexPaths.remove(at: index)
}
else{ //select it if the row is deselected
cell?.imageView?.image = UIImage(named: "check.png")
selectedIndexPaths.append(indexPath)
}
}

请注意,细胞正在被再利用!请在 cellForRowAt 方法中做同样的检查。

关于ios - 双击 UITableView 单元格应该转到以前的状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44281577/

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