2017-01-19 8 views
-2

複数の選択肢(年)または(すべて)を選択し、残りのセルは消えます(チェックマーク)。 ユーザーがそれぞれキャンセルできないようにしたい(チェックマーク)。Checkmark TableView Multiple Swift 3.0の追加方法

この私のコードは: -

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
let cell = tableView.cellForRow(at: indexPath) 
cell?.accessoryType = .checkmark 
} 

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! yearsCell 
cell.years?.text = Years[indexPath.row] 
return cell } 

enter image description here

enter image description here

+0

詳細を入力する必要があります。あなたは何を望んでいますか、何があなたのために働いていませんか? – Magnas

+0

@Magnas私は次のようになります: - 1.ユーザーは任意の(年)またはキャンセルを選択できます。 2.ユーザーの選択(すべて)がすべてのセルの消えます(チェックマーク)。 3.ユーザーはすべてをキャンセルすることはできません(チェックマーク)既定値(すべて)。 –

答えて

2

これは、selectedindexPathを配列に格納してから、tableViewをリロードして、選択したセルを変更して、セルの再利用に関する問題を回避します。テスト済み)。

var selectedIndexPathArray = Array<NSIndexPath>() 

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
    let cell = tableView.cellForRow(at: indexPath) 
    selectedIndexPathArray.append(indexPath) 
    tableView.reloadData() 
} 

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! yearsCell 
    cell.years?.text = Years[indexPath.row] 
    cell.accessoryType = .none 
    for sip in selectedIndexPathArray { 
     if indexPath == sip { 
      cell.accessoryType = .checkmark 
     } 
    } 
    return cell 
} 
+0

いいえ、できません。複数選択肢がありません –

+0

@MajedDkahellalahよろしくお願いします。私はそれを複数の年を選択できるように配列に変更しました。 – Wez

+0

'selectedIndexPathArray'(ユーザーがセルを再びタップする場合)に重複を避けるためにいくつかのロジックを書くこともできます。また、選択した年をユーザーが削除できるようにすることも検討してください。 – Wez

0

セルは再利用可能です。たとえば、1行目を表示するセルは、スクロールダウン時に10行目を表示するために使用されます。

  • didSelect:配列[indexpath.row] =配列[indexpath.row]
  • cellForRow:setCheckmark(配列[indexpathしたがって、アレイ・ストアを行ごとにチェックマーク状態、のようないくつかのコードが必要.row])

MORE:Googleの方法と理由細胞はそうのようにテーブルビュー

1

で再利用されます。

SWIFT 3

// To checkmark the cell 
let cell = tableView.cellForRow(at:indexPath.row) 
cell.accessoryType = .checkmark 

// To make it unselectable 

indexPath.rowには選択セル、およびreturn falseの行である場合tableView:willSelectRowAtIndexPath:メソッドを使用して確認してください。

関連する問題