Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 为什么我的UISwitches状态总是打开?_Ios_Swift_Uitableview_Uiswitch - Fatal编程技术网

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

Ios 为什么我的UISwitches状态总是打开?,ios,swift,uitableview,uiswitch,Ios,Swift,Uitableview,Uiswitch,我有一个带有UILabel和UISwitch的自定义单元格的TableView,我想在选择行时打开/关闭单元格的开关 问题是,当我在didselectRowatinedexpath()方法中获取UISwitch时,状态始终为on(即使我在emulator中将其设置为OFF),而setOn()方法不起任何作用 我在想,获取的开关可能不是好的开关,但当我显示单元格的标签时,它是好的开关 我的手机: class CourseResourceCell: UITableViewCell { @I

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

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

这里,什么是课程资源列表和课程资源?请给出details@Zell我的代码错误是使用未解析标识符“CourceSource List”?我的代码错误是使用未解析标识符“CourceSource List”?下面的答案中的CourseSourcesList和CourseSource是什么?请提供详细信息