2015-12-21 25 views
3

我在PFTableView中使用UISwitch。當我打開一個UISwitch時,它觸發不同單元中的另一個開關。這裏是PFTableViewCell的代碼。UISwtich觸發UITableView中的另一個UISwitch事件

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) 
    } 
} 

Output of tableView

+0

你應該保存你的開關狀態的細胞的對象,而不是讓它默認控制你的開關,'dequeueReusableCellWithIdentifier'東西是重疊的非編碼開關,可以存在其他的方式做,但林不知道:d – Tj3n

+0

當這種情況發生時,兩個開關都在屏幕上,還是您必須滾動才能發現錯誤觸發的開關?如果是這樣,那是因爲其他人說的細胞再利用。您應該每次都將單元重置爲乾淨狀態。理想情況下,通過使用屬性觀察器來分配「熱點」屬性可以處理更新單元狀態。 –

+0

@NicolasMiari我必須向下滾動才能看到第二個開關。但代碼在IBAction.Can請在代碼中詳細說明您的答案。 –

回答

1

這是因爲表格單元格得到與dequeueReusableCellWithIdentifier重用。您需要考慮到這一點並每次重置開關。

+0

我不認爲這可能是解決方案。 –

+0

hello @Michael。你能告訴我怎樣才能在代碼中實現這一點。 –

1
//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) 
} 
} 
1

所以,按照您的評論:

@NicolasMiari我需要向下滾動以查看第二switch.But的 代碼是IBAction.Can請您詳細闡述你的答案代碼。 - NISHANT Dongare

...看來該小區被重用,所以開關巋然不動「上」,因爲當使用電池來顯示索引#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(_: forIndexPath:);後者將自動進行實例化你的情況下,不存在細胞池中重用,讓你的代碼變得更短和更簡單)

或者,你可以做,當你指定的細胞照顧它模型object:

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.) 
     } 
    } 
+0

你好@NicolasMiari。我嘗試了兩種方法。 '如果細胞==零{// 細胞是全新的,無需清理: 電池= NearestHsCell(風格:UITableViewCellStyle.Default,reuseIdentifier: 「tempHsCell」) } 其他{// 單元正被重複使用;重置所有子視圖/狀態等。 cell.switch.on = false }' 它正在工作。但不利的一面是,當我向下滾動,再次向上滾動選定的Swtich也被取消選中。 –

+1

這是因爲你的數據模型沒有「記住」開關狀態。我的猜測是,當您翻轉開關時,與單元格關聯的熱點對象應該存儲該狀態。下次將同一熱點對象分配給新單元格時,該單元格必須相應地更新其子視圖(即將該開關設置爲開)。是否有意義? –

+1

單元格被重用以提高效率;在任何時候只有scrren所需的實例數量存在。您的單元格必須能夠恢復與顯示數據模型的給定行相關聯的任何狀態。 –

0

我在一組UITableViewCell單元內有多個UISwitch類似的問題。這是「addTarget」堆積在他人之上。解決它之前刪除它們。

cell.optionSwitch.on = configManager.distanceMetric; 
[cell.optionSwitch removeTarget:self action:nil 
forControlEvents:UIControlEventTouchUpInside]; 
[cell.optionSwitch addTarget:self action:@selector(updateDistanceMetric:) 
forControlEvents:UIControlEventTouchUpInside];