我有以下代码,通过按住然后将项目移动到不同的位置来重新排序 TableView 中的项目,如附图所示。
该代码非常适合按索引路径排序的对象。
我的模型已经改变,现在按日期排序。关于如何修改代码以将日期重新分配给交换的单元格以便将它们保存在核心数据中的任何想法。
//MARK : - Cell Re-Ordering
struct Drag {
static var placeholderView: UIView!
static var sourceIndexPath: NSIndexPath!
static var sourceDate: NSDate!
}
func handleLongPress(gesture: UILongPressGestureRecognizer) {
let point = gesture.locationInView(tableView)
let indexPath = tableView.indexPathForRowAtPoint(point)
switch gesture.state {
case .Began:
if let indexPath = indexPath {
let cell = tableView.cellForRowAtIndexPath(indexPath)!
Drag.sourceIndexPath = indexPath
var center = cell.center
Drag.placeholderView = placeholderFromView(cell)
Drag.placeholderView.center = center
Drag.placeholderView.alpha = 0
tableView.addSubview(Drag.placeholderView)
UIView.animateWithDuration(0.25, animations: {
center.y = point.y
Drag.placeholderView.center = center
Drag.placeholderView.transform = CGAffineTransformMakeScale(1.05, 1.05)
Drag.placeholderView.alpha = 0.95
cell.alpha = 0
}, completion: { (_) in
cell.hidden = true
})
}
case .Changed:
guard let indexPath = indexPath else {
return
}
var center = Drag.placeholderView.center
center.y = point.y
Drag.placeholderView.center = center
if indexPath != Drag.sourceIndexPath {
/////////////// Swap the Index Path ///////////////
swap(&self.weekDayModel.exerciseItems[indexPath.row], &self.weekDayModel.exerciseItems[Drag.sourceIndexPath.row]) //Previous Model Swapping
tableView.moveRowAtIndexPath(Drag.sourceIndexPath, toIndexPath: indexPath)
Drag.sourceIndexPath = indexPath
}
default:
if let cell = tableView.cellForRowAtIndexPath(Drag.sourceIndexPath) {
cell.hidden = false
cell.alpha = 0
UIView.animateWithDuration(0.25, animations: {
Drag.placeholderView.center = cell.center
Drag.placeholderView.transform = CGAffineTransformIdentity
Drag.placeholderView.alpha = 0
cell.alpha = 1
}, completion: { (_) in
Drag.sourceIndexPath = nil
Drag.placeholderView.removeFromSuperview()
Drag.placeholderView = nil
})
}
}
}
func placeholderFromView(view: UIView) -> UIView {
UIGraphicsBeginImageContextWithOptions(view.bounds.size, false, 0.0)
view.layer.renderInContext(UIGraphicsGetCurrentContext()!)
let image = UIGraphicsGetImageFromCurrentImageContext() as UIImage
UIGraphicsEndImageContext()
let snapshotView : UIView = UIImageView(image: image)
snapshotView.layer.masksToBounds = false
snapshotView.layer.cornerRadius = 0.0
snapshotView.layer.shadowOffset = CGSizeMake(-5.0, 0.0)
snapshotView.layer.shadowRadius = 5.0
snapshotView.layer.shadowOpacity = 0.4
return snapshotView
}
我的核心数据模型:
import CoreData
@objc(graduationModel)
class graduationModel: NSManagedObject {
// Insert code here to add functionality to your managed object subclass
}
extension graduationModel {
@NSManaged var Name: String?
@NSManaged var graduation: String?
@NSManaged var date: NSDate?
}
非常感谢任何想法。
最佳答案
排序核心数据对象
Core Data 的结果是
NSSet
,即无序对象。我想权衡速度。您只能按您提供的属性排序。更改模型并在 UI 中反射(reflect)新顺序,而不是更改 UI 并希望传播到 Core Data。使用
NSFetchResultController
将处理所有 UI 刷新:您需要注意的是核心数据的完整性。
型号
您只能使用与您的对象关联的属性进行排序。考虑使用增量唯一标识符,创建 NSDate
,修改 NSDate
,所有这些都将帮助您稍后对内容进行排序。
用户界面
利用 NSFetchedResultsController
大大减少您的工作。
同时使用适当模型和 NSFetchedResultsController
进行排序的示例 here .
关于ios - 在 TableView 中重新排序按日期排序的单元格项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35156311/