gpt4 book ai didi

uitableview - 在 UITableViewCell 自定义类的 UIView 属性上添加 UISwipeGestureRecognizer

转载 作者:可可西里 更新时间:2023-11-01 01:06:52 25 4
gpt4 key购买 nike

我有一个自定义的 UITableViewCell 类,它的初始化方法如下所示:

init(style: UITableViewCellStyle, reuseIdentifier: String!) {
super.init(style: style, reuseIdentifier: reuseIdentifier)

let swipeLeft: UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: "swipedLeft")
let swipeRight: UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: "swipedRight")

swipeLeft.direction = UISwipeGestureRecognizerDirection.Left
swipeRight.direction = UISwipeGestureRecognizerDirection.Right

self.topLayerView?.addGestureRecognizer(swipeLeft)
self.topLayerView?.addGestureRecognizer(swipeRight)
}

self.topLayerView 是一个 IBOutlet。问题是当我添加 gestureRecognizers 时,self.topLayerViewnil

如果我在 init 方法中写这样的东西:

if self.topLayerView? {
println("NOT empty")
} else {
println("empty")
}

它总是给我“空”。所以,我的问题是:将这段代码放在哪里比较合适?

如有任何帮助,我们将不胜感激!

最佳答案

Init 在您的 View 的生命周期中太早,无法期望任何 IBOutlets 已设置。我建议将您的代码移动到单元格的 awakeFromNib() 方法,该方法在接口(interface)文件完全加载后被调用。您可以在 NSObject UIKit Additions Reference 中找到有关此方法和其他方法的更多信息.

override func awakeFromNib() {
super.awakeFromNib()

let swipeLeft: UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: "swipedLeft")
let swipeRight: UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: "swipedRight")

swipeLeft.direction = UISwipeGestureRecognizerDirection.Left
swipeRight.direction = UISwipeGestureRecognizerDirection.Right

self.topLayerView?.addGestureRecognizer(swipeLeft)
self.topLayerView?.addGestureRecognizer(swipeRight)
}

关于uitableview - 在 UITableViewCell 自定义类的 UIView 属性上添加 UISwipeGestureRecognizer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24316602/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com