【发布时间】:2018-12-07 19:01:01
【问题描述】:
我有一个应用程序,在该应用程序中,长按后会从集合视图中删除单元格。删除发生在视图模型中,它看起来像这样:
func deleteFood(forIndexPath indexPath: IndexPath, completion: @escaping ([FoodBySections], [FoodBySections]) -> ()) {
let objectToDeleteName = self.foodBySections[indexPath.section][indexPath.row].name!
let oldData = foodBySections
CoreDataHelper.sharedInstance.deleteFoodFromFridge(foodName: objectToDeleteName)
foods = CoreDataHelper.sharedInstance.fetchFoods()
//newData
foodBySections = FoodBySections.split(foods: foods!)
//return data to view controller for collection view
completion(oldData, foodBySections)
}
在视图控制器中代码如下所示:
@objc func deleteFood(gesture: UILongPressGestureRecognizer!) {
if gesture.state != .ended {
return
}
let point = gesture.location(in: self.collectionView)
if let indexPath = self.collectionView?.indexPathForItem(at: point) {
self.foodViewModel?.deleteFood(forIndexPath: indexPath, completion: { [weak self] (oldData, newData) in
guard let self = self else { return }
//receive data from view model via closure
self.collectionView.animateItemAndSectionChanges(oldData: oldData, newData: newData)
})
}
}
这是一个好习惯吗?还是 RxSwift 是个好主意?
【问题讨论】: