0

我不确定下面的代码有什么问题。我所做的只是获取表中的所有行并将它们标记为已选中,如果它们已被选中,我将取消选择它们。每当我尝试这个时,一些行索引路径是不正确的。每次总会有一些索引路径丢失。我究竟做错了什么?

let visibleRows = self.tableView.indexPathsForVisibleRows

for row in visibleRows! {
    if(self.sections[row.section].files[row.row].type != "cloud"){
        let cell = self.tableView.cellForRowAtIndexPath(row)
        cell?.setSelected(action, animated: true)
        tableView.selectRowAtIndexPath(row, animated: true, scrollPosition: UITableViewScrollPosition.Top)
        if(action  == true){
            cell?.accessoryType = .Checkmark
        } else {
            cell?.accessoryType = .None
            NSNotificationCenter.defaultCenter().postNotificationName("disableOptions", object: nil)
        }
    }
}
4

1 回答 1

0

执行以下步骤

  1. 创建并初始化一个MutableArray,可能在viewDidLoad.
  2. 添加indexPath选定的cell. 在选择delegate方法中这样做celldidSelectRowAtIndexPath方法
  3. 检查indexPathincellForRowAtIndexPath数组中的 。如果indexPath存在则作为选中处理cell,否则作为未选中处理cell
  4. indexPath如果方法中需要,从数组中删除didSelectRowAtIndexPath
于 2016-10-26T05:40:26.917 回答