自定义表格视图单元格不滑动

Posted

技术标签:

【中文标题】自定义表格视图单元格不滑动【英文标题】:Custom Table View Cell Not Swiping 【发布时间】:2015-05-20 18:51:36 【问题描述】:

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

在 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 中,我尝试使单元格可滑动,但没有任何反应,并且没有调用平移手势识别器。

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



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

【问题讨论】:

尝试在单元格的init方法中设置userInteractionEnabled = true 没用 :( 单元格已经可以交互了,因为我可以点击它们并且它们会突出显示@zellb 您必须设置tableView:canEditRowAtIndexPath 方法以启用滑动删除:developer.apple.com/library/ios/documentation/UIKit/Reference/…: 不要以为就是这样,因为文档说:“如果未实施此方法,则假定所有行都是可编辑的。” @DejanSkledar 你能检查一下是否调用了gestureRecognizerShouldBegin吗? 【参考方案1】:

终于明白了!添加手势识别器时,请在 awakeFromNib 中执行此操作。 这是工作代码:

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 而烦恼。朋友建议的另一种替代解决方案是将滚动视图添加到单元格内容视图并从那里开始工作。希望这可以帮助! 并感谢所有帮助过的人!

【讨论】:

以上是关于自定义表格视图单元格不滑动的主要内容,如果未能解决你的问题,请参考以下文章

自定义表格单元格不显示任何内容 - Swift 2

自定义表格单元格不符合 NilLiteralConvertible Swift Xcode 6 Beta 5

自定义表格视图单元格上的多个图像(Swift)

防止自定义 UITableViewCells 中的多个平移手势

自定义公式 Google 电子表格

如何在自动布局中滑动表格视图单元格?