2016-08-01 249 views
0

我從火力點數據分析,像這樣一個實現代碼如下:火力地堡(SWIFT)重新排序表

for snap in snapshotSetlists { 
       if let songsDict = snap.value as? Dictionary<String, AnyObject>{ 
        let key = snap.key 
        let song = SongModel(songKey: key, dictionary: songsDict) 
        self.songData.append(song) 
       } 
      } 

如何管理,使表的永久重新排序與

func tableView(tableView: UITableView, moveRowAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath) { 

我知道,可以通過子進行排序,但我無法弄清楚如何在本地重新排序表之後更新索引。

謝謝!

回答

1

我看到你還沒有得到答案 - 我正在處理同樣的事情,我認爲我正在做的事情將是你唯一的選擇。

您需要首先確保源查詢是按字段排序的,而不是按鍵排序的默認值。改變鍵比字段更難,也可能不是你想要做的。在我們的應用程序中,我們爲每條記錄添加了一個「權重」字段,並在其上添加了queryOrderedByChild。任何排序鍵字段都可以。

完成此操作後,現在可以重新排序,並遵循大多數其他iOS應用程序使用的相同技術。有幾種方法可以做到這一點,但是如果您沒有龐大的數據集,我發現最簡單的方法是製作數據集的可變數組副本。如果您的桌子使用FirebaseUIFUIArray,這很容易 - 只需撥打[array items]即可爲您製作該副本。

從那裏,使用標準的iOS數組重新排序調用來移動數組中的項目,然後重新計算每個項目的排序關鍵字。最後,將調整後的排序鍵設置爲Firebase。粗略的僞代碼:

// Make a temp array to do the move in 
NSMutableArray *temp = [[myData items] mutableCopy]; 
[temp removeObjectAtIndex:sourceIndexPath.row]; 
[temp insertObject:interest atIndex:destinationIndexPath.row]; 

// Renumber the items in Firebase 
NSInteger sortKey = 0; 
for (FIRDataSnapshot *entry in temp) { 
    [[entry.ref child:@"mySortKey"] setValue:@(sortKey)]; 
    sortKey++; 
} 

根據您將表格連接到Firebase的方式,它應該在此時自動更新。