2017-08-04 68 views
0

我有一個TableView從核心數據中獲取數據。Swift TableView插入新行

在數據保存後,它們將作爲第一行插入。

這裏是我用來保存數據的代碼(我沒有使用獲取控制器,因爲我沒有獲取任何數據,只是加載它們,這是因爲我加載的日期來自一個一對多關係)

swimToConnect.addToMaterialsLocal(materialLocal) 
    ad.saveContext() 

     // Adding the new row to update the tableView 
     let MyMaterialsVC = self.presentingViewController?.presentingViewController?.presentingViewController as! MyMaterialsVC 
     MyMaterialsVC.myMaterials.insert(materialLocal, at: 0) 
     MyMaterialsVC.tableView.insertRows(at: [NSIndexPath(row: 0, section: 0) as IndexPath], with: .none) 
     MyMaterialsVC.dismiss(animated: true, completion: nil) 
    } 

所以我想知道是否有方法插入按日期排序的行。 我已經命令他們按日期,像這樣:

var swim: SwimminPool! { 
    didSet { 
     myMaterials = (swim.materialsLocal?.allObjects as! [MaterialLocal]).sorted(by: {$0.createdAtLocal! > $1.createdAtLocal!}) 
    } 
} 

在哪裏創建是一個日期選擇器,用戶添加的日期。 當我保存新數據顯然他們顯示在第一行,但如果我解僱了控制器,然後回來,然後數據顯示根據日期順序。

有沒有辦法在我保存後立即以正確的順序排列數據?

謝謝。

+0

你爲什麼不重新加載表視圖只是增加了數據之後。正如我在加載視圖時所瞭解的那樣,表視圖按日期排序顯示數據。因此,而不是在表視圖中插入行,只需將數據保存在覈心數據中,然後重新加載表視圖 –

+0

您的意思是刪除這兩行:MyMaterialsVC.myMaterials.insert(materialLocal,位於:0) MyMaterialsVC.tableView .insertRows(在:[NSIndexPath(row:0,section:0)as IndexPath],其中:.none) – Marco

+0

當您將其保存在覈心數據中並應用排序時,在此之後您可以重新加載數據表視圖,其數據源是您的核心數據。 –

回答

0

更改爲以下

swimToConnect.addToMaterialsLocal(materialLocal) 
    ad.saveContext() 

     // Adding the new row to update the tableView 
     let MyMaterialsVC = self.presentingViewController?.presentingViewController?.presentingViewController as! MyMaterialsVC 
     MyMaterialsVC.myMaterials.insert(materialLocal, at: 0) 
     MyMaterialsVC.myMaterials = (swim.materialsLocal?.allObjects as! [MaterialLocal]).sorted(by: {$0.createdAtLocal! > $1.createdAtLocal!}) 
     MyMaterialsVC.tableView.reloadData() 
     MyMaterialsVC.dismiss(animated: true, completion: nil) 
    } 
+0

它仍然顯示該項目作爲第一次解僱後保存...然後在正確的位置,如果我退出並返回到桌面視圖 – Marco

+0

這現在完美的作品!謝謝! :) – Marco

+0

很高興它幫助@Marco –

0

的最佳方式對於這個問題,最有效的一個方法是使用NSFetchedResultsController。它負責有效地插入和刪除項目,並使用NSPredicate對它們進行排序。 您可以在Xcode中創建主詳細信息應用程序模板並選中核心數據框,然後Xcode使用FetchedResultsController創建示例應用程序,您可以在其中看到它的工作原理。

+0

我是新來的迅速,所以我仍在練習......我使用fetcheresult控制器來獲取主要實體...但我不知道如何使用它時得到一對多實體.. – Marco

+0

您創建了一個'FetchedResultController'的實例,併爲其設置了一個實體。它只能顯示一個實體。通常情況下,你顯示一個單一的實體作爲你的表格視圖行。如果你想獲得關係值,你可以讓'frc'給你實體並使用像'(myBook.author')這樣的點狀來獲得它的關係。一個對象數組。 – Alan

0

這是一個小的通用函數,它返回新項目的排序數組中的插入索引。好處是你不需要重新加載整個表格視圖。

func orderedInsertIndex<T : Comparable>(array: [T], item: T) -> Int { 
    var index = array.count 
    while index > 0 && item < array[index-1] { 
     index -= 1 
    } 
    return index 
} 

而且你可以使用它:

let insertionIndex = orderedInsertIndex(array: MyMaterialsVC.myMaterials.map{ $0.createdAtLocal! }, item: materialLocal.createdAtLocal!) 
MyMaterialsVC.myMaterials.insert(materialLocal, at: insertionIndex) 
MyMaterialsVC.tableView.insertRows(at: [IndexPath(row: insertionIndex, section: 0)], with: .none) 
MyMaterialsVC.dismiss(animated: true, completion: nil) 

旁註:有一種天然的結構IndexPath斯威夫特3

+0

哎Vadian!謝謝!!總是幫助我! :) – Marco