自定义表视图单元格不刷卡 [英] Custom Table View Cell Not Swiping

查看:102
本文介绍了自定义表视图单元格不刷卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个TableViewController,它有一个自定义原型单元格,在Storyboard中设置了标识符regularCell。 TableViewController属于TimelineTableViewController.swift类,单元格是TimelineTableViewCell.swift.swift类,它们都在Storyboard中设置。

I have a TableViewController that has one custom prototype cell with the identifier "regularCell" set in Storyboard. The TableViewController is of the class TimelineTableViewController.swift and the cell is of the class TimelineTableViewCell.swift.swift both set in Storyboard.

在TimelineTableViewController.swift中:

In TimelineTableViewController.swift:

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.events.count
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = self.tableView.dequeueReusableCellWithIdentifier("regularCell", forIndexPath: indexPath) as! TimelineTableViewCell
    cell.selectionStyle = .None
        let event = events[indexPath.row]
        cell.content.text = event.content
        cell.name.text = event.name
        cell.metadata.text = event.metadata
        return cell
    }

}

在TimelineTableViewCell.swift中,我尝试使细胞可以滑动,但没有任何反应,并且没有调用平移手势识别器。

And in TimelineTableViewCell.swift I try to make the cell swipe-able but nothing happens and the pan gesture recognizer isn't being called.

import UIKit

class TimelineTableViewCell: UITableViewCell {

var originalCenter = CGPoint()
var deleteOnDragRelease = false

@IBOutlet weak var name: UILabel!
@IBOutlet weak var content: UILabel!
@IBOutlet weak var metadata: UILabel!

override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)
    var recognizer = UIPanGestureRecognizer(target: self, action: "handlePan:")
    recognizer.delegate = self
    addGestureRecognizer(recognizer)
}

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

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
}

}

有人会碰巧知道我做错了吗?表中的单元格加载完美,但它们只是不可刷卡,好像我从未添加过任何东西。非常感谢任何帮助或提示

Would anyone happen to know what I am doing wrong? The cells in the table load perfectly but they just aren't swipeable as if I never added anything. Any help or hints are greatly appreciated

推荐答案

最后想出来了!添加手势识别器时,请在awakeFromNib中执行此操作。
这是工作代码:

Finally figured it out! When adding the gesture recognizer do so in awakeFromNib. Here's the working code:

override func awakeFromNib() {
        super.awakeFromNib()

        var panGestureRecognizer = UIPanGestureRecognizer(target: self, action: "handlePan:")
        panGestureRecognizer.delegate = self
        addGestureRecognizer(panGestureRecognizer)

    }
    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
    }

不要过分重写init。朋友建议的另一种替代解决方案是将滚动视图添加到单元格内容视图并从那里开始工作。希望这可以帮助!
感谢所有帮助过的人!

Do not bother with overriding init. Another alternative solution as suggested by a friend, would be to add a scrollview to the cell content view and work from there. Hope this helps! And thanks to all who helped!

这篇关于自定义表视图单元格不刷卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆