我有以下代码,通过按住然后将项目移动到其他位置(如附加的图像中所示)来重新排序表格视图中的项目。
该代码适用于按索引路径排序的对象。
我的模型已更改,现在按日期订购商品。有关如何修改代码以更改为将日期重新分配给已交换的单元格的任何想法,以便将它们保存在核心数据中。
//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?
}
非常感谢任何想法。
答案 0 :(得分:1)
排序核心数据对象
Core Data的结果是NSSet
,即无序对象。我想,以速度换取权衡。您只能按照您可以使用的属性进行排序。
更改模型并在UI中反映新订单,而不是更改UI并希望传播到Core Data。使用NSFetchResultController
将处理所有UI刷新:您需要注意的是您的核心数据完整性。
<强>模型强>
您只能使用与对象关联的属性进行排序。考虑使用增量唯一标识符,创建NSDate
,修改NSDate
,所有这些都可以帮助您稍后对内容进行排序。
用户界面
利用NSFetchedResultsController
大大减少您的工作。
使用正确的模型和NSFetchedResultsController
here进行排序的示例。