Ios UITableView单元格选择动画和NSFetchedResultsController
我有一个Ios UITableView单元格选择动画和NSFetchedResultsController,ios,swift,uitableview,core-data,uiviewanimation,Ios,Swift,Uitableview,Core Data,Uiviewanimation,我有一个UITableViewController,它允许多重选择,并显示存储在CoreData中的数据模型 当用户点击该行时,我们需要将其设置为选定状态(更改布局、淡入某些元素等)。问题是,当用户点击该行并将其选中时,模型会被更新(因为我们也将选中的项存储在模型中)。由于NSFetchedResultController在重新加载整个表时中止了我们奇特的动画 让它更清楚。下面是我在表视图中调用的configureCell方法:cellforrowatinexpath。将setSelected方
UITableViewController
,它允许多重选择,并显示存储在CoreData中的数据模型
当用户点击该行时,我们需要将其设置为选定状态(更改布局、淡入某些元素等)。问题是,当用户点击该行并将其选中时,模型会被更新(因为我们也将选中的项存储在模型中)。由于NSFetchedResultController在重新加载整个表时中止了我们奇特的动画
让它更清楚。下面是我在表视图中调用的configureCell
方法:cellforrowatinexpath
。将setSelected
方法设置为animated:false
的原因在于,当用户滚动表格时,将已选择的单元格设置为正确的状态
func configureCell(cell: MealCell, indexPath: NSIndexPath) {
let menuItem = self.fetchedResultsController.objectAtIndexPath(indexPath) as! MenuItem
cell.name = menuItem.name
// some cell initialisation
//we have this code to draw cells selected when user scrolls our table view. We don't need animation here.
if menuItem.isSelected {
cell.setSelected(true, animated: false)
} else {
cell.setSelected(false, animated: false)
}
cell.setNeedsUpdateConstraints()
cell.updateConstraintsIfNeeded()
}
所有动画都发生在MealCell
类的setSelected
方法中
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
extraView.hidden = !selected
let extraViewAlpha: CGFloat = selected ? 1.0 : 0.0
self.extraViewWidthConstraint.constant = selected ? 38 : 0
if animated {
UIView.animateWithDuration(0.5, delay: 0.0, options: UIViewAnimationOptions.CurveEaseInOut, animations: { () -> Void in
self.layoutIfNeeded()
}, completion: { completed -> Void in
UIView.animateWithDuration(0.5, animations: { () -> Void in
self.extraView.alpha = extraViewAlpha
})
})
}
else {
self.layoutIfNeeded()
self.extraView.alpha = extraViewAlpha
}
}
最好是更改存储selectedItems的方式,以防止fetchedResultsController将更改通知委托。解决方法是将fetchedResultsController.delegate临时设置为nil,并在动画结束后将其设置回原位。最好的方法是更改存储selectedItems的方式,以防止fetchedResultsController将更改通知代理。解决方法是将fetchedResultsController.delegate临时设置为nil,并在动画结束后将其设置回原位。您是否考虑过存储选定单元格的临时
NSDictionary
,然后仅在用户离开视图时保存数据?这应该允许您寻求的灵活性。您是否考虑过存储选定单元格的临时NSDictionary
,然后仅在用户离开视图时保存数据?这应该允许您所寻求的灵活性。