2017-04-21 4 views
0

私はthis linkを見直しました。UITableviewCellはカスタムチェックマーク状態を1つだけトグルします - スウィフト3

1つの行を選択しようとしていますが、選択するとラベルにチェックマークが追加されます。既存のチェックマークがある間に別の行が選択された場合は、selectedIndexPath変数に格納されている前のチェックマークのチェックを外します。私がやっている何

enter image description here

テーブルビューを通じて数回スクロールしたときにそれはしかし、初めに働く

、私は時折、この画像で示されるべきではない選択したセルを参照してくださいユーザがセルを選択:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 

    if let cell = tableView.cellForRow(at: indexPath) as? CustomCell { 
     let customCell = customCellData[indexPath.row] 
     customCell.toggleSelected() 
     cell.configureCheckmark(with: customCell) 
    } 

    if let oldIndexPath = selectedIndexPath, let cell = tableView.cellForRow(at: oldIndexPath) as? CustomCell, oldIndexPath.row != indexPath.row { 
     let customCell = customCellData[oldIndexPath.row] 
     customCell.toggleSelected() 
     cell.configureCheckmark(with: customCell) 
    } 


    if let selected = selectedIndexPath, selected.row == indexPath.row { 
     selectedIndexPath = nil 
     tableView.deselectRow(at: indexPath, animated: true) 
    } else { 
     selectedIndexPath = indexPath 
    } 

} 

とcellForRowAtために:(?それがモデルにselectedIndexPath及び状態を確認する冗長である)

CustomCellで
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCell", for: indexPath) as! CustomCell 
    let customCell = customCellData[indexPath.row] 
    cell.customCell = customCell 
    if selectedIndexPath == indexPath { 
     cell.checkLabel.text = "✔️" 
    } else { 
     cell.checkLabel.text = "" 
    } 

    return cell 
} 

し、最終的に:CustomCellData

var customCell: CustomCell? { 
    didSet { 
     if let customCell = customCell { 
      configureCheckmark(with: customCell) 
     } 
    } 
} 

func configureCheckmark(with customCell: CustomCellData) { 
    if customCell.isSelected { 
     checkLabel.text = "✔️" 
    } else { 
     checkLabel.text = "" 
    } 
} 

次のように私は状態を切り替え:

class CustomCellData { 
    var isSelected = false 

    func toggleSelected() { 
     isSelected = !isSelected 
    } 
} 

私は任意のヘルプ、何をすべきか、この上で私の頭を掻くとわからないのです素晴らしいことだ。

答えて

1

最も簡単な解決策は、これが適切にすべての可視セルを更新didSelectRowAt

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) 
{ 
    selectedIndexPath = indexPath 
    tableView.reloadData() 
} 

に削減することです。

あるいは細胞が既にcellForRowAt

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) 
{ 
    if selectedIndexPath != indexPath { 
     let indexPathsToReload = selectedIndexPath == nil ? [indexPath] : [selectedIndexPath!, indexPath] 
     selectedIndexPath = indexPath 
     tableView.reloadRows(at: indexPathsToReload, with: .none) 
    } else { 
     selectedIndexPath = nil 
     tableView.reloadRows(at: [indexPath], with: .none) 
    } 
} 

コードは残りを行い、選択された場合にのみ影響を受ける行とチェックを更新し、より洗練されたバージョン。

+0

これは機能しますが、選択した同じセルのチェックを外したい場合は、どうすればよいでしょうか?同じ場合は 'selectedIndexPath'を' nil'に設定しますか? – Simon

+0

私は答えを更新しました。 Btw: 'CustomCellData'のチェックマークを処理するためのカスタムコードも必要ありません。 – vadian

関連する問題