为什么我的UISwitches状态始终为ON?

时间:2015-08-06 09:19:51

标签: ios swift uitableview uiswitch

我有一个带有UILabel和UISwitch的自定义单元格的TableView,我想在选择行时打开/关闭单元格的开关。

问题是当我在didSelectRowRowAtIndexPath()方法中获取UISwitch时,状态始终为ON(即使我在模拟器中将其设置为OFF),并且setOn()方法没有&#39什么都不做......

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

我的手机:

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)
    }

1 个答案:

答案 0 :(得分:2)

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

一些具体实施:

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)
}