2014-11-25 19 views
-1

當我使用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

回答

0

這裏就是我所做的:

  1. 製造IndexPaths的字典。使用didSelectRowAtIndexPathdidDeselectRowAtIndexPath將鍵/值對添加到字典中並將其刪除。

  2. 裏面viewWillAppear我以前var indexForLoop = myDictionary.values.array來作爲indexPath設置爲傳遞到我的For Loop

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

謝謝Lyndsey的幫助!

相關問題