ios - 可重复使用的 tableview 单元格相互影响而无需交互

标签 ios swift uitableview uiswitch

我第一次尝试完全以编程方式在 Swift 中制作应用程序,但在使用 UITableViews 时遇到了问题。 我创建了一个名为“exerciseCell”的自定义可重用单元格,其中有一个 UILabel、一个 UIImageview 和一个 UISwitch。

我的问题是,当我选择第 0 行的开关时,它也会打开第 3 行和第 6 行的开关。这对每个单元格都是一样的,它们分别影响大约三个单元格。 此表格 View 的目的是能够选择您想要的练习列表,但每个练习都需要能够独立选择。

这是单元格的代码:

class exerciseCell: UITableViewCell {

override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)
    setupViews()
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

var cellSelected:Bool = false

let excersiseLabel: UILabel = {
    let label = UILabel()
    label.translatesAutoresizingMaskIntoConstraints = false
    label.textColor = UIColor.red
    label.textAlignment = NSTextAlignment.center
    return label
}()
let excersiseImage: UIImageView = {
    let image = UIImageView()
    image.backgroundColor = UIColor.blue
    image.translatesAutoresizingMaskIntoConstraints = false
    return image
}()

let thisSwitch: UISwitch = {
    let thisSwitch = UISwitch()
    thisSwitch.isOn = false
    thisSwitch.translatesAutoresizingMaskIntoConstraints = false
    return thisSwitch

}()

func setupViews() {
    addSubview(excersiseLabel)
    addSubview(excersiseImage)
    addSubview(thisSwitch)
    addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[v0]|", options: NSLayoutFormatOptions.alignAllCenterX, metrics: nil, views: ["v0": excersiseLabel]))
    addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-101-[v0(100)]-101-|", options: .alignAllCenterX, metrics: nil, views: ["v0": excersiseImage]))
   addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-120-[v0(100)]-120-|", options: NSLayoutFormatOptions.alignAllCenterX, metrics: nil, views: ["v0": thisSwitch]))

    addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-12-[v0(30)]-8-[v1(100)]-12-[v2(30)]-12-|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": excersiseLabel, "v1": excersiseImage, "v2": thisSwitch]))

}

还有cellForRow如下:

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

    var thisCell = UITableViewCell()

    if tableView == self.tableView {
        let aCell = tableView.dequeueReusableCell(withIdentifier: "thirdCellID", for: indexPath) as! exerciseCell
        aCell.excersiseLabel.text = "\(mobility[indexPath.row])"
        aCell.thisSwitch.tag = indexPath.row
        thisCell = aCell
    }
    return thisCell

}

如何让单元格独立响应用户?如果需要说明,我可以提供更多我的代码。

最佳答案

由于表格 View 会重复使用单元格,因此它们不会保留其开关设置。您将需要一个外部结构来跟踪选择了哪些单元格。

单元将需要让您的 Controller 知道它的开关何时被切换(可能通过委托(delegate)),此时您将更新您的(例如)BOOL 数组。

然后,在 cellForRowAtIndexPath 中,您需要一些代码来将正确的值应用到单元格。像这样的东西:

cell.switch.isOn = cellSwitchStates[indexPath.row]

关于ios - 可重复使用的 tableview 单元格相互影响而无需交互,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42394962/

相关文章:

ios - 如何将 array.indexOf 与协议(protocol)一起使用?

swift - Firebase Cloud Functions 不会从 iOS 应用程序中触发

ios - 如何根据下载的图像大小创建动态大小的行高?

iOS - DrawRect 性能问题

ios - 使用 AFNetworking 验证自签名证书

ios - 从 AppDelegate 推送 View Controller

html - 正则表达式替换 HTML 页面内的内容

ios - 从大标题页转换到小标题页时出现导航间隙

iphone - UITableView懒加载优化

ios - 如何使用 Apple Watch 中的加速度计数据?