【发布时间】:2018-01-30 05:01:19
【问题描述】:
我有一个UILabel 在TableView 内,我想在用户点击时将 UILabel 的颜色更改为红色。我正在使用UITapGestureRecognizer,在点击UILabel 时,我可以获得UILabel 的内容,但我无法获得实际的UILabel,因为据我所知,UIGesture 中不能有参数功能。
这是我的代码,它可以帮助你搞清楚
class HomeProfilePlacesCell: NSObject {
var Post = [String]()
@objc func PostTap(_ sender: UIGestureRecognizer) {
print(Post[(sender.view?.tag)!])
}
func HomeProfilePlaceTVC(_ tableView: UITableView, cellForRowAt indexPath: IndexPath, streamsModel : streamModel,HOMEPROFILE: HomeProfile, controller: UIViewController) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "HomeTVC", for: indexPath) as! HomeTVC
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(PostTap(_:)))
tapGesture.delegate = self as? UIGestureRecognizerDelegate
cell.post.addGestureRecognizer(tapGesture)
cell.post.text = streamsModel.Posts[indexPath.row]
cell.post.tag = indexPath.row
Post = streamsModel.Posts
return cell
}
}
我的功能是 PostTap 每当用户点击UILabel 这是 cell.post 然后我可以在 PostTap 中阅读它的内容> 但是为了改变 UILabel 的颜色,我必须将 let cell 常量传递给 PostTap 函数。
无论如何我可以做到这一点或解决方法吗?我是 Swift 新手
【问题讨论】:
-
在你的情况下:
call.delegate = controller -
试试这个@objc func PostTap(_ sender: UIGestureRecognizer) { sender.view?.backgroundColor = .red }
标签: ios swift swift3 uigesturerecognizer