Swift - selectRowAtIndexPath not working with indexPathForSelectedRows

1.7k views Asked by At

When I use tableView.indexPathForSelectedRow(), calling selectRowAtIndexPath works perfectly. However, I am selecting multiple rows thus I am trying to use table.View.indexPathForSelectedRows() as [NSIndexPath]

Swift Compiler Error: NSIndexPath is not a subtype of NSIndexPath

Error has been resolved.

Edit Added For Loop, multiple selection still not happening.

//I initialized saveSelection globally
var saveSelection : [NSIndexPath]?



override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{   
    saveSelection = (drugTableView.indexPathsForSelectedRows() as [NSIndexPath])   
}

override func viewWillAppear(animated: Bool)
{
    if !selectedDrugs.isEmpty
    {
        for save : NSIndexPath in saveSelection! as [NSIndexPath]
        {

            self.drugTableView.selectRowAtIndexPath(save, animated: true, scrollPosition: UITableViewScrollPosition.None)
        }
    }

}

Thank you for your help.

1

There are 1 answers

0
cheelo007 On

Here's what I did:

  1. Made a dictionary of IndexPaths. Used didSelectRowAtIndexPath and didDeselectRowAtIndexPath to add and remove key/value pairs to the dictionary.

  2. Inside viewWillAppear I used var indexForLoop = myDictionary.values.array to as the indexPath set to pass into my For Loop

  3. Crossed my fingers and hoped for the best

thank you Lyndsey for your help!