【发布时间】:2016-02-02 14:38:03
【问题描述】:
我有以下代码,通过按住然后将项目移动到不同位置来重新排序表格视图中的项目,如附图所示。
该代码非常适用于按索引路径排序的对象。
我的模型已经改变,现在按日期订购商品。关于如何修改代码以更改以将日期重新分配给交换的单元格的任何想法,以便将它们保存在核心数据中。
//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?
}
非常感谢任何想法。
【问题讨论】:
-
好的..您必须在数据库中添加一个属性,例如每个项目/行的排名/顺序。一旦您要使用任何单元格(行)移动/更改,然后还要替换它们之间的相互排名。
-
@Gagan_iOS 可以。我只是不确定如何在创建下一个对象时跟踪排名/顺序属性。在添加新项目时,我必须知道最后创建的排名/订单属性,以防止订单/排名属性发生冲突。除非您知道解决该问题的更好方法,否则可能比使用日期更难。 ;)
-
有道理。 @SwiftArchitect。您的建议是否与之前的评论一致 - 为模型中的每个实体添加排名属性还是您有其他建议?
标签: ios swift uitableview core-data