UIPanGestureRecognizer左右tableviewcell

时间:2016-09-25 06:21:34

标签: ios swift uitableview uipangesturerecognizer

目前,我正在开发一个iOS应用程序,它使用UITableviewCell的子类,使用UIPanGestureRecognizer左右滑动,但FavoritesTableViewCell无法正常工作并出错。

  

错误:致命错误:不支持NSCoding

UITableViewCell.swift

class FavoritesTableViewCell: UITableViewCell {

@IBOutlet weak var favimage: UIImageView!

@IBOutlet weak var texten: UILabel!

@IBOutlet weak var textfn: UILabel!

@IBOutlet weak var price: UILabel!

@IBOutlet weak var likeimage: UIImageView!

var originalCenter = CGPoint()
var deleteOnDragRelease = false
    override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
}
override func setSelected(selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)
    // Configure the view for the selected state
}

required init(coder aDecoder: NSCoder) {
    fatalError("NSCoding not supported")
}
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)
    let recognizer = UIPanGestureRecognizer (target: self, action: "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 {
        // the frame this cell had before user dragged it
        let originalFrame = CGRect(x: 0, y: frame.origin.y,
                                   width: bounds.size.width, height: bounds.size.height)
        if !deleteOnDragRelease {
            // if the item is not being deleted, snap back to the original location
            UIView.animateWithDuration(0.2, animations: {self.frame = originalFrame})
        }
    }
}

override func gestureRecognizerShouldBegin(gestureRecognizer: UIGestureRecognizer) -> Bool {
    if let panGestureRecognizer = gestureRecognizer as? UIPanGestureRecognizer {
        let translation = panGestureRecognizer.translationInView(superview!)
        if fabs(translation.x) > fabs(translation.y) {
            return true
        }
        return false
    }
    return false
}}

and my favourite storyboard

2 个答案:

答案 0 :(得分:0)

评论或删除此内容:

required init(coder aDecoder: NSCoder) {
    fatalError("NSCoding not supported")
}

答案 1 :(得分:0)

如果您通过代码创建单元格,那么您的代码就可以了。


如果您使用情节提要创建单元格,则会调用required init(coder aDecoder: NSCoder) {

当然,fatalError("NSCoding not supported")


U也可以使用此

required init?(coder: NSCoder) {
    super.init(coder: coder)
}

awakeFromNib()

中进行初始化
override func awakeFromNib() {
    super.awakeFromNib()

    let recognizer = UIPanGestureRecognizer (target: self, action: "handlePan:")
    recognizer.delegate = self
    addGestureRecognizer(recognizer)
        }