好的,這似乎是一個非常簡單的問題,但我似乎無法有效地解決它或在網上找到任何有正確答案的東西。所以這里。
我有一個帶有原型單元的 tableViewController 運行自定義類。在原型單元格內,我有一個復選標記的影像,當用戶選擇該單元格時我想顯示它并在選擇另一個單元格時隱藏該影像。我有一種可行的方法,但它需要我在桌子上重新加載資料,這似乎效率很低。所以一定有更好的方法吧?
我的代碼...
//CUSTOM CLASS (trimmed code down to just show relevant code)
class GoalsTableViewCell: UITableViewCell {
@IBOutlet weak var gNameLabel: UILabel!
@IBOutlet weak var gIsSelectedImage: UIImageView!
}
}
//TABLE VIEW CONTROLLER (trimmed it down to just show relevant code)
class GoalsTableVC: UITableViewController {
var selectedGoalId = ""
var selectedInd = 0
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let gCell = tableView.dequeueReusableCell(withIdentifier: "gCell", for: indexPath) as! GoalsTableViewCell
gCell.gNameLabel.text = goalsData[indexPath.row].gName
if indexPath.row == selectedInd {
gCell.gIsSelectedImage.isHidden = false
} else {
gCell.gIsSelectedImage.isHidden = true
}
return gCell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
selectedGoalId = goalsData[indexPath.row].id!
selectedInd = indexPath.row
tableView.reloadData()
}
}
uj5u.com熱心網友回復:
可以僅重新加載受影響的行
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
selectedGoalId = goalsData[indexPath.row].id!
let indexPathsToReload = [indexPath, IndexPath(row: selectedInd, section: 0)]
selectedInd = indexPath.row
tableView.reloadRows(at: indexPathsToReload, with: .automatic)
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/515075.html
標籤:迅速合适的视图didselectrowatindexpath
