iOS swift : Maintaining Toggle Button State In A Cell

标签 ios swift xcode tableviewcell

我在一个单元格中有一个按钮作为切换按钮,用于 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/

相关文章:

cocoa - 关于Delegate Protocol声明效果的问题

ios - 需要帮助解决 Apple 崩溃报告

ios - 解析本地数据存储不显示固定数据

ios - 尽管构建成功,但 Swift 模拟器不会在 Xcode 中启动

ios - 从语音到文本的数学评估

ios - 如何使用 arc4random 从数组中选择随机 SKShapeNode?

xcode - 设置兼容所有 iDevices 的背景图片

objective-c - iOS UITextView 中的字距调整

iphone - 如何使表格 View 可滚动

ios - 坚持在 Swift 和 MapKit 中使用 MKPinAnnotationView()