我有一个带有带有 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/