我有以下代碼,通過按住然後將項目移動到不同的位置來重新排列表格視圖中的項目,如附圖所示。在表格視圖中按日期排序的單元格項目
該代碼完美適用於按索引路徑排序的對象。
儘管我的模型已更改,但現在按日期排序。關於如何修改代碼以更改以將日期重新分配給交換單元以便將它們保存在覈心數據中的任何想法。
//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這可以工作。我只是不確定如何在創建下一個對象時跟蹤排名/訂單屬性。添加一個新項目後,我將不得不知道最後創建的排名/訂單屬性,以防止衝突的排序/排名屬性。除非你知道解決這個問題的更好方法,否則可能比使用日期要困難一些。 ;) – Gugulethu
有道理。 @SwiftArchitect。您的建議是否符合之前的評論 - 爲模型中的每個實體添加排名屬性還是您有其他建議? – Gugulethu