为什么我的 UISwitches 状态总是打开?

Why my UISwitches states are always ON?

我有一个带有带有 UILabel 和 UISwitch 的自定义单元格的 TableView,我想 ON/OFF 在选择一行时切换单元格。

问题是当我在 didSelectRowRowAtIndexPath() 方法中获取我的 UISwitch 时,状态是否总是打开(即使我在模拟器中将其设置为关闭),而 setOn() 方法没有什么都不做...

我在想可能是取到的开关不是那个好开关,但是当我显示一个单元格的标签时,它是一个好开关...

我的手机:

class CourseResourceCell: UITableViewCell {

    @IBOutlet weak var mSwitch: UISwitch!
    @IBOutlet weak var mNameCourseResourceLabel: UILabel!

}

我在 tableView 中的代码:

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        println(courseResourceCellAtIndexPath(indexPath).mNameCourseResourceLabel.text)
        var courseResourceSwitch = courseResourceCellAtIndexPath(indexPath).mSwitch
        if courseResourceSwitch.on {
            println("ON to OFF")
            courseResourceSwitch.tintColor = UIColor.blackColor()
            courseResourceCellAtIndexPath(indexPath).mSwitch.setOn(false, animated: true)
        }
        else {
            println("OFF to ON")
            courseResourceSwitch.setOn(true, animated: true)
        }
        tableView.deselectRowAtIndexPath(indexPath, animated: true)
    }

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        return courseResourceCellAtIndexPath(indexPath)
    }

    func courseResourceCellAtIndexPath(indexPath:NSIndexPath) -> CourseResourceCell {
        let cell = self.mTableView.dequeueReusableCellWithIdentifier(CourseResourceReuseIdentifier) as! CourseResourceCell
        setCourseResourceNameForCell(cell, indexPath: indexPath)
        return cell
    }

    func setCourseResourceNameForCell(cell :CourseResourceCell, indexPath :NSIndexPath) {
        let courseResource = courseResourcesList[indexPath.row] as CourseResource
        if var label = cell.mNameCourseResourceLabel {
            label.text = courseResource.name
        }

        let leftMarginConstraint = NSLayoutConstraint(item: cell.mSwitch, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: cell.mSwitch.superview, attribute: NSLayoutAttribute.LeadingMargin, multiplier: CGFloat(courseResource.level!), constant: 1)
        cell.mSwitch.superview!.addConstraint(leftMarginConstraint)
    }

您遇到的问题与可重复使用的电池有关。通过调用 dequeueReusableCellWithIdentifier,您对表视图中的所有记录使用相同的单元格,并且直接更改视图状态也可能会影响其他视图。要解决这个问题,您必须以某种方式在数据源中保存单元格状态,例如。在 CourseResource 中声明一个变量 isOn 并在选择一个单元格后更改此变量的值并在之后重新加载该单元格。

一些具体实现:

func setCourseResourceNameForCell(cell :CourseResourceCell, indexPath :NSIndexPath) {
    let courseResource = courseResourcesList[indexPath.row] as CourseResource
    if var label = cell.mNameCourseResourceLabel {
        label.text = courseResource.name
    }

    if courseResource.isOn{
        cell.mSwitch.setOn(true, animated: true)
    }else{
        cell.mSwitch.setOn(false, animated: true)
    }

    let leftMarginConstraint = NSLayoutConstraint(item: cell.mSwitch, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: cell.mSwitch.superview, attribute: NSLayoutAttribute.LeadingMargin, multiplier: CGFloat(courseResource.level!), constant: 1)
    cell.mSwitch.superview!.addConstraint(leftMarginConstraint)
}

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
  var courseResource = courseResourcesList[indexPath.row] as CourseResource      

  if courseResource.isOn {
        courseResource.isOn = false
  }else{
       courseResource.isOn = true
  }

  tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
  tableView.deselectRowAtIndexPath(indexPath, animated: true)
}