Ios UISwtich正在UITableView中触发另一个UISwitch事件
我正在Ios UISwtich正在UITableView中触发另一个UISwitch事件,ios,swift,uitableview,uiswitch,Ios,Swift,Uitableview,Uiswitch,我正在PFTableView中使用UISwitch。当我打开一个UI开关时,它会触发不同单元中的另一个开关。以下是PFTableViewCell的代码 override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject ? ) - > PFTableViewCell { var cell = tableView.dequeueReus
PFTableView
中使用UISwitch
。当我打开一个UI开关时,它会触发不同单元中的另一个开关。以下是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
一起重用。您需要考虑到这一点,并每次重置开关。因此,根据您的评论:
//need to maintain Switch status of every cell , using datasource , initially datasource initialize with all elements "NO"
override func tableView(tableView: UITableView, cellForRowAtIndexPathindexPath: 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*/
if arrDatasource.objectAtIndex(indexPath.row) as! String == "YES" {
switch.on = true
} else {
switch.on = false
}
cell.hotspot = object
switch.tag = indexpath.row
return cell
}
@IBAction func userSelectHotspot(sender: UISwitch) {
if hotspotSwitch.on {
arrDatasource.setObject("YES", atIndexedSubscript:sender.tag)
selectedHotspot[(self._hotspot ? .objectId) !] = self._hotspot
} else {
arrDatasource.setObject("NO", atIndexedSubscript:sender.tag)
let key = self._hotspot!.objectId!
selectedHotspot.removeValueForKey(key)
}
}
@NicolasMiari我必须向下滚动才能查看第二个开关 代码在IBAction中。请用代码详细说明您的答案 西山东加雷酒店 …该单元格似乎正在被重用,因此开关保持“打开”,因为当该单元格用于显示索引#0处的行时,您点击了它 要避免在重用单元格时保留任何以前的状态,应始终在重用前重置该状态:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject ? ) - > PFTableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("tempHsCell") as!NearestHsCell!
if cell == nil {
// Cell is brand new, no need to clean up:
cell = NearestHsCell(style: UITableViewCellStyle.Default, reuseIdentifier: "tempHsCell")
}
else{
// Cell is being reused; reset all subviews/state etc.
cell.switch.on = false
}
/*Assign Object to cell*/
cell.hotspot = object
return cell
}
(作为旁注,我强烈建议您改为使用dequeueReusableCellWithIdentifier(u:forIndexPath:)
;如果池中没有可重用的单元格,它会自动为您进行实例化,因此您的代码会变得更短、更简单)
或者,您可以在指定模型对象时让单元处理它:
class NearestCell : UITableViewCell {
var hotSpot:<(The Class of hotspot)> {
didSet {
// (Update state of the switch, cell's title label, etc.
// based on the new value of hotspot.)
}
}
class NearestCell:UITableViewCell{
var热点:{
迪塞特{
//(更新开关状态、单元格标题标签等)。
//基于热点的新值。)
}
}
我遇到了一个类似的问题,在一组UITableViewCell单元格中有多个UISwitch。是“addTarget”堆叠在其他UISwitch的上面。在解决问题之前将其移除
cell.optionSwitch.on = configManager.distanceMetric;
[cell.optionSwitch removeTarget:self action:nil
forControlEvents:UIControlEventTouchUpInside];
[cell.optionSwitch addTarget:self action:@selector(updateDistanceMetric:)
forControlEvents:UIControlEventTouchUpInside];
您应该通过将开关状态存储在单元格的对象中来控制开关,而不是让它默认值,
dequeueReusableCellWithIdentifier
内容与您的非编码开关重叠,可能还有其他方法,但我不确定:发生这种情况时,是否需要在屏幕上同时显示两个开关,或者您必须滚动以发现错误触发红色开关?如果是这样,那是因为如其他人所说的单元重复使用。您应该每次都将单元重置为干净状态。理想情况下,分配热点
属性可以通过使用属性观察器来更新单元状态。@NicolasMiari我必须向下滚动以查看第二个开关。但是代码正在iAction中。您能不能在代码中验证你的答案。我不认为这是解决方案。hello@Michael。你能告诉我如何在代码中实现这一点吗。hello@NicolasMiari。我尝试了这两种方法。如果cell==nil{//cell是全新的,无需清理:cell=NearestHsCell(样式:UITableViewCellStyle.Default,reuseIdentifier:“tempHsCell”)}其他{//Cell正在被重用;重置所有子视图/状态等。Cell.switch.on=false}
它正在工作。但不利的一面是,当我向下滚动并再次向上滚动时,所选的开关也被取消选择。这是因为您的数据模型没有“记住”“切换状态。我的猜测是,当您翻转开关时,与单元格关联的热点对象应存储该状态。下次您将同一热点对象分配给新单元格时,该单元格必须相应地更新其子视图(即,打开开关)。”。这有意义吗?为了提高效率,可以重用单元格;在任何时候,只有scrren上所需的实例数存在。您的单元格必须能够还原与显示数据模型给定行相关的任何状态。在您的情况下,当交换机更改状态时,您应该修改单元格热点的属性(在关联的@IBAction内),以便下次将该热点分配给不同的重用单元时,它可以将其开关设置为适当的状态并保持一致性。