2016-10-07 3 views
0

私は非常にばかげた問題がありますが、結果が出ないいくつかのヒントを試しました。私は大規模なテーブル(いくつかの行をチェック)との関係で選択リストが必要です。スウィフトテーブル、選択された行が失われて表示されない

コードでは、一貫性のあるアイデアを持つフラグメントですが、このリストには「可視」行のみが表示されます。最初と最後を選択すると、myループは画面上に表示される行のみを表示します。下に私は結果を列挙したが、0,1,2が選択されているがリストには載っていない。

ありがとうございました!ポールの提案に続き

override func viewDidLoad() { 
    super.viewDidLoad() 
    myTableList.setEditing(true, animated: true) // show tick 
    myTableList.delegate = self 
    myTableList.dataSource = self 
    myTableList.allowsMultipleSelectionDuringEditing = true 
... 
} 

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
    print("selected \(indexPath.item)") 
} 
@IBAction func listme(sender: UIBarButtonItem) { 
    for i in 0..<myTableList.numberOfSections { 
     for j in 0..<myTableList.numberOfRowsInSection(i) { 
      print("cell #: \(j)") 
      if let cell = myTableList.cellForRowAtIndexPath(NSIndexPath(forRow: j, inSection: i)) { 
       print("cell \(j) \(cell.selected)") 
      } 
     } 
    } 
} 


@ === console results === @ 
selected 0 
selected 1 
selected 2 
selected 13 
selected 14 
selected 15 
cell #: 0 
cell #: 1 
cell #: 2 
cell #: 3 
cell #: 4 
cell #: 5 
cell #: 6 
cell #: 7 
cell 7 false 
cell #: 8 
cell 8 false 
cell #: 9 
cell 9 false 
cell #: 10 
cell 10 false 
cell #: 11 
cell 11 false 
cell #: 12 
cell 12 false 
cell #: 13 
cell 13 true 
cell #: 14 
cell 14 true 
cell #: 15 
cell 15 true 
=========================== 
+0

セルはデータ上のビューであり再利用されるため、セルオブジェクト自体を使用して状態を追跡することはできません。セルがオフスクリーンになると、 'cellForRowAtIndexPath'は' nil'を返します。あなたの選択肢を 'IndexSet'で追跡するか、テーブルビューの' indexPathsForSelectedRows'プロパティを使用してください – Paulw11

+0

PaulW11ありがとう、何度か分かりません:) accessoryTypeをリセットするこのコードは私をエラーに導きます: if cell = myTableList.cellForRowAtIndexPath(NSIndexPath(forRow:j、inSection:i)){cell.accessoryType = .None} – MSamsoniuk

答えて

0

、私は変更されました:私のレポートに

var cellchecked: [DarwinBoolean] = [] 

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
    print("checked \(indexPath.item)") 
    cellchecked[indexPath.item] = true 
} 
func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) { 
     print("none \(indexPath.item)") 
     cellchecked[indexPath.item] = false 
} 

簡単なコードをあなたたちに感謝し、質問を解決!

if cellchecked[j] { .... } 
関連する問題