2017-08-04 67 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