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

标签 ios swift uitableview uiswitch

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

问题是当我在 didSelectRowRowAtIndexPath() 方法中获取我的 UISwitch 时,状态是否始终为 ON(即使我在模拟器中将其设置为 OFF),并且 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,您对 TableView 中的所有记录使用相同的单元格,直接更改 View 状态也可能会影响其他 View 。要解决这个问题,您必须以某种方式在数据源中保存单元格状态,例如。在 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)
} 

关于ios - 为什么我的 UISwitches 状态总是打开?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31851743/

相关文章:

ios - ios在应用程序结算中。自动续订后是否需要发送新的收据?

ios - Swift 4 覆盖 func viewDidAppear(_ 动画 : Bool) not triggering when i dissmiss viewcontroller

ios - 未找到模块 'apple_sign_in'

swift - 类型为 'representedObject' 的属性 'AnyObject?' 无法覆盖类型为 'Any?' 的属性

ios - 如何在 Swift 代码中调用另一个类中的 SOAP 请求/响应?

ios - 如何将 TableView 单元格从最新到最旧而不是从最旧到最新排序

ios - (Swift) 在主 UINavigationController 内嵌套的 UINavigationController 和 UITabController 之间切换

swift - 线性回归 - Swift 中的加速框架

ios - 如何在编辑时对 UITableViewCell 进行动画处理?

ios - 按标签查找单元格的indexPath