自定义表视图单元格不刷卡

时间:2015-05-20 18:51:36

标签: ios uitableview swift tableviewcell

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

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

}

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

1 个答案:

答案 0 :(得分:2)

终于想通了!添加手势识别器时,请在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。朋友建议的另一种替代解决方案是将滚动视图添加到单元格内容视图并从那里开始工作。希望这可以帮助! 感谢所有帮助过的人!