0

I am not sure what is wrong with the code below. All I'm doing is getting all the rows in the table and marking them as selected, and if they're already selected, I'm deselecting them. Whenever I try this, some of the row index paths are incorrect. There are always a few index paths that are missed every time. What am I doing wrong?

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)
        }
    }
}
Aaron
  • 6,466
  • 7
  • 37
  • 75
Prashanth Kumar B
  • 566
  • 10
  • 25

1 Answers1

0

Do the following steps

  1. Create and initialise a MutableArray, probably in the viewDidLoad.
  2. Add the indexPath of a selected cell. Do this in the delegate method for cell selection, didSelectRowAtIndexPath method
  3. Check for the indexPath in the cellForRowAtIndexPath in array. If the indexPath exists then handle as the selected cell, otherwise as not selected cell.
  4. Remove the indexPath from array if required in didSelectRowAtIndexPath method.
Sudheer Kolasani
  • 283
  • 5
  • 28
KGen
  • 50
  • 3
  • An `NSMutableIndexSet` is better than an array – Paulw11 Oct 26 '16 at 05:42
  • Thanks Paulw11, I just provide the steps to implement, no matter what is the collection object :) – KGen Oct 26 '16 at 05:45
  • I am sorry, the issue was not with selecting all, but however I do understand that your way works, perfectly, thank you for your response, I asked another question which describes the exact scenario can you take a look at it, http://stackoverflow.com/questions/40255463/multi-select-is-not-working-properly-in-ios – Prashanth Kumar B Oct 26 '16 at 06:49