【发布时间】:2015-12-21 04:53:52
【问题描述】:
我在PFTableView 中使用UISwitch。当我打开一个 UISwitch 时,它会触发不同单元格中的另一个开关。这是PFTableViewCell的代码。
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject ? ) - > PFTableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("tempHsCell") as!NearestHsCell!
if cell == nil {
cell = NearestHsCell(style: UITableViewCellStyle.Default, reuseIdentifier: "tempHsCell")
}
/*Assign Object to cell*/
cell.hotspot = object
return cell
}
// Code for UISwtich Action
@
IBAction func userSelectHotspot(sender: UISwitch) {
if hotspotSwitch.on {
selectedHotspot[(self._hotspot ? .objectId) !] = self._hotspot
} else {
let key = self._hotspot!.objectId!
selectedHotspot.removeValueForKey(key)
}
}
【问题讨论】:
-
您应该通过将开关状态存储在单元格的对象中而不是让它默认来控制您的开关,
dequeueReusableCellWithIdentifier的东西与您的非编码开关重叠,可能还有其他方法可以做,但我不确定: D -
发生这种情况时两个开关都在屏幕上,还是您必须滚动才能发现错误触发的开关?如果是这样,那是因为其他人说的细胞重用。您应该每次都将单元格重置为清洁状态。理想情况下,分配您的
hotspot属性可以通过使用属性观察器来更新单元格状态。 -
@NicolasMiari 我必须向下滚动才能查看第二个开关。但代码在 IBAction 中。您能否在代码中详细说明您的答案。
标签: ios swift uitableview uiswitch