Swift - selectRowAtIndexPath不能与indexPathForSelectedRows一起使用

问题描述:

当我使用tableView.indexPathForSelectedRow()时,调用selectRowAtIndexPath可以很好地工作。不过,我从而选择多行,我试图用table.View.indexPathForSelectedRows() as [NSIndexPath]Swift - selectRowAtIndexPath不能与indexPathForSelectedRows一起使用

Swift Compiler Error: NSIndexPath is not a subtype of NSIndexPath

错误已得到解决。

编辑增加了循环,多选还没有发生。

//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) 
     } 
    } 

} 

谢谢你的帮助。

+0

'saveSelection'是一个'NSIndexPath'数组,但'selectRowAtIndexPath'需要一个'NSIndexPath'作为第一个参数。 – 2014-11-25 02:28:46

+0

所以没有办法重新选择多个选择? – cheelo007 2014-11-25 02:29:51

+0

我没有那么说。我只是说你不能在该函数中插入数组。也许循环访问数组,并在每个NSIndexPaths上执行selectRowAtIndexPath会得到你想要的结果... – 2014-11-25 02:38:32

这里就是我所做的:

  1. 制造IndexPaths的字典。使用didSelectRowAtIndexPathdidDeselectRowAtIndexPath将键/值对添加到字典中并将其删除。

  2. 里面viewWillAppear我以前var indexForLoop = myDictionary.values.array来作为indexPath设置为传递到我的For Loop

  3. 越过我的手指和最好的希望

谢谢Lyndsey的帮助!