【发布时间】:2017-03-17 20:44:41
【问题描述】:
我有一个UITableViewDataSource,下面是
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: OutletDetails.cellIdentifier) as! OutletDetails
cell.selectionStyle = .none
cell.isUserInteractionEnabled = true
cell.location = "Some location will be here"
let tap = UITapGestureRecognizer(target: self, action: #selector(locationClicked))
tap.cancelsTouchesInView = false
tap.numberOfTapsRequired = 1
cell.location.addGestureRecognizer(tap)
}
其中cell.location 是UILabel 对象。我在这里要做的是检测UILabel 上的点击事件。我查看了整个互联网,每个人都在建议这种方法,但是,这段代码在我的情况下不起作用。 locationClicked 方法根本没有被调用。谁能告诉我我的代码有什么问题?
编辑
还有一件事,以记忆方式这样做是个好主意吗?我的意思是,如果我们有一个很长的列表,那么每个单元格都会生成许多 UIGestureRecognizer 对象。这是因为在滚动项目时会经常调用该方法。
【问题讨论】:
-
我的回答好运吗?有意义吗?
-
@toddg 不幸的是它对我不起作用,我最终将标签更改为按钮并将其格式化,使其看起来像标签
-
哦,我明白了。您应该发布您的解决方案并接受它,以便其他人可以从中受益。
标签: ios swift uitableview uilabel uitapgesturerecognizer