我在一个单元格中有一个按钮作为切换按钮,用于 checkin 俱乐部的成员。当我登记成员(member)时,我需要按钮的状态在滚动后保持打开状态,但它又会关闭。这是 cellForRow 方法:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = self.membersTableVw.dequeueReusableCell(withIdentifier: "CellMembersForCoach", for: indexPath) as! CellMembersForCoach
let member = members[indexPath.row]
cell.setMember(member)
cell.cellController = self
return cell
}
这是自定义单元格类中我切换按钮的部分
@IBOutlet weak var checkBtn: UIButton!
@IBAction func setAttendance(_ sender: Any){
// toggle state
checkBtn.isSelected = !checkBtn.isSelected
}
切换有效,但滚动表格后,按钮状态变回原始状态。任何建议表示赞赏。
最佳答案
发生这种情况是因为您正在重复使用这些单元格。
您需要跟踪哪些单元格已被选中。也许在您的成员(member)类(class)中。然后,当您在 cellForRowAt
中时,您应该检查之前是否已选择此单元格并为您的按钮设置正确的状态。
关于iOS swift : Maintaining Toggle Button State In A Cell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44665735/