2016-12-21 8 views
0

私はUITableViewを使用して、セルを選択するときに話す言語を選択し、他のセルを選択したテーブルでスクロールダウンできるようにします。UITableViewは複数のセルを選択したときに複数の選択を可能にする

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell:UITableViewCell! 
    cell = tableView.dequeueReusableCell(withIdentifier: "language",for: indexPath) 
    cell?.textLabel?.text = languageValues[indexPath.row] 
} 

func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) { 
    if let cell = tableView.cellForRow(at: indexPath) { 
     cell.accessoryType = .none 
} 

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

gif explain the problem

答えて

0

あなたの問題は、細胞がキャッシュされ、再利用してしまうことがあります。これは、状態情報を格納するのに悪い場所であることを意味します。

あなたのlanguageValues配列には、各オブジェクトが名前文字列と選択されたブール値を持つオブジェクトを含めることをお勧めします。誰かがセルを選択または選択解除すると、ブール値を変更して、影響を受けたセル(またはテーブル)のデータを照合してリロードします。

このようにしてcellForRowAt indexPathaccessoryTypeの値が変更されます。

関連する問題