我使用UITableViewController.swift和UITableViewCell.swift構建了一個包含CoreData的應用程序。在TableViewCell中刪除行後在TableViewController中刪除行
我試圖通過在UITableViewCell.swift中使用UIPanGestureRecognizer來刪除行,如同在Clear to-do應用程序中一樣。我可以平移行左側和右側,但我不知道如何獲得這些選定行的indexPath,並在所有數據所在的UITableViewController.swift中將其刪除。
EduDicTableViewCell.swift:
override func awakeFromNib() {
super.awakeFromNib()
let recognizer = UIPanGestureRecognizer(target: self, action: #selector(EduDicTableViewCell.handlePan(_:)))
recognizer.delegate = self
addGestureRecognizer(recognizer)
}
//MARK: - horizontal pan gesture methods
func handlePan(recognizer: UIPanGestureRecognizer) {
// 1
if recognizer.state == .Began {
// when the gesture begins, record the current center location
originalCenter = center
}
// 2
if recognizer.state == .Changed {
let translation = recognizer.translationInView(self)
center = CGPointMake(originalCenter.x + translation.x, originalCenter.y)
// has the user dragged the item far enough to initiate a delete/complete?
deleteOnDragRelease = frame.origin.x < -frame.size.width/2.0
}
// 3
if recognizer.state == .Ended {
let originalFrame = CGRect(x: 0, y: frame.origin.y,
width: bounds.size.width, height: bounds.size.height)
if deleteOnDragRelease {
print("send it")
} else {
UIView.animateWithDuration(0.2, animations: {self.frame = originalFrame})
print("Bounced Back")
}
}
}
感謝您的閱讀!
這是完美的!感謝數百萬! –
非常好。謝謝! – Ibrahim