Swift表,丢失所选行但不可见

时间:2016-10-07 22:10:04

标签: ios uitableview swift2

我有一个非常愚蠢的问题,但我尝试了几个没有结果的提示。我需要一个关于大表的选择列表(勾选一些行)。

在代码中,一个片段具有更一致的想法,但是这个列表只有“可见”行,如果你选择第一个和持续,我的循环列表只有屏幕上可见的行。下面我列出了结果,选择了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
===========================

1 个答案:

答案 0 :(得分:0)

根据Pauls的建议,我改为:

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] { .... }