2017-02-07 17 views
0

私はSwiftを初めて使用しています。私はテーブルビューで複数の行選択を無効にしたいと考えています。私はアプローチの異なるタイプを実装しようとし、私の最終的なコードブロックは動作していません、以下です。 ご協力いただきありがとうございます。SwiftのTableviewで複数行選択を無効にする

override func viewDidLoad() { 
     super.viewDidLoad() 
     companyTableView.allowsMultipleSelection = false 
} 

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

     if let cell = tableView.cellForRow(at: indexPath) { 
      cell.accessoryType = .checkmark; 
     } 

     tableView.deselectRow(at: indexPath, animated: true) 
     if let cell = tableView.cellForRow(at: indexPath) { 
      cell.accessoryType = .checkmark 
     } 

     tableView.deselectRow(at: indexPath, animated: true) 
     if let cell = tableView.cellForRow(at: indexPath) { 
      if selectedCells.contains(indexPath.row) { 
       selectedCells.remove(indexPath.row) 
       cell.accessoryType = .none 
      } else { 
       selectedCells.insert(indexPath.row) 
       cell.accessoryType = .checkmark 
      } 
     } 
    } 
+2

'allowsMultipleSelection = false'をが正常に動作しています。 –

+0

デフォルトでは、単一選択はストーリーボードでのみ選択されています。ストーリーボード - >あなたのテーブルビューを選択し、属性インスペクタの下に、選択肢なし、単一選択、複数選択があります。実行時に変更したくない場合は、ストーリーボードから設定することができます。 'allowsMultipleSelection'のデフォルト値もfalseです。 – Priyal

+1

'cellForRowAtindexPath'コードを表示します。 –

答えて

0

このコードを試してください:

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

     if !selectedCells.contains(indexPath){ 
      selectedCells.add(indexPath) 

      if let cell = tableView.cellForRow(at: indexPath) { 
       cell.accessoryType = .checkmark 
      } 
     } 
    } 


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

     if selectedCells.contains(indexPath){ 
      selectedCells.remove(indexPath) 

      if let cell = tableView.cellForRow(at: indexPath) { 
       cell.accessoryType = .none 
      } 
     } 
    } 

そしてcellForRowAt使用してこの:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

     if selectedCells.contains(indexPath){ 
       cell.accessoryType = .checkmark 
     } 
     else{ 
      cell.accessoryType = .none 
     } 

     return cell 
    } 
+0

こんにちはポーランド、私はすでにストーリーボードで 'シングルセレクション'を選択しましたが、私の問題は解決しませんでした。 – gozdebal

+0

次に、問題はコードの 'didSelectRowAt'部分にあると思います。あなたのシングルセレクションは機能していますが、チェックマークを設定する方法には何らかの欠陥があります。 – Poles

+0

@gozdebal:私の更新されたコードを確認してください。 – Poles

0

あなたの選択/選択解除コールに問題があるようです。このようなものを使用します。 は、ここで私は(細胞の総数の数に等しいその大きさ)selectedCellsに配列

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
     tableView.cellForRow(at: indexPath)?.accessoryType = .checkmark 
     if filteredStudents.count > indexPath.row { 
      selectedCells[indexPath.row] = true 
     } 
    } 

    func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) { 
     tableView.cellForRow(at: indexPath)?.accessoryType = .none 
     if selectedStudents.count > indexPath.row { 
      selectedCells[indexPath.row] = false 
     } 
    } 
関連する問題