-1

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.

cheelo007
  • 51
  • 12
  • `saveSelection` is an array of `NSIndexPath`s, but `selectRowAtIndexPath` requires a single `NSIndexPath` as the first argument. – Lyndsey Scott Nov 25 '14 at 02:28
  • so there is no way to re-select multiple selections? – cheelo007 Nov 25 '14 at 02:29
  • I didn't say that. I just said you can't insert an array into that function. Perhaps looping through the array and performing selectRowAtIndexPath on each of the NSIndexPaths will get you the result you want... – Lyndsey Scott Nov 25 '14 at 02:38
  • I was just trying that as you were typing... lol. trying to figure it out now. If you have any example code, that would be very helpful. Thank you for responding – cheelo007 Nov 25 '14 at 02:40
  • I added the for loop, but still no Luck – cheelo007 Nov 25 '14 at 03:16
  • Yeah, the selectRowAtIndexPath line was a definite issue, but I suspected it might not be causing that particular error... Is the error specifically attached to the self.drugTableView.selectRowAtIndexPath line? – Lyndsey Scott Nov 25 '14 at 03:21
  • I apologize, I just edited the post. I no longer have the error. The `for loop` println's successfully yet, it will not select the rows when I the return the the tableView. – cheelo007 Nov 25 '14 at 03:28
  • I assume you were wanting `selectRowAtIndexPath` to result in a call to `didSelectRowAtIndexPath`? Well, that's not automatic in this case and you'll have to do it manually. Here's a good answer explaining why: http://stackoverflow.com/a/2726140/2274694 – Lyndsey Scott Nov 25 '14 at 03:32

1 Answers1

1

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!

cheelo007
  • 51
  • 12