I have a label in the tableview cell . label have text end of the text has a button that button represent for user like . everything is fine but problem is hitForLike
not fire .button click event not fire. do i miss anything
我在tableview单元格中有一个标签。标签有文本文本末尾有一个按钮,该按钮代表用户喜欢。一切都很好,但问题是hitForLike not fire .button click event not fire。我什么都想念
var titleLabel : UILabel = {
var label = UILabel()
label.font = UIFont.systemFont(ofSize: 21)
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()
func hitForLike(_ sender : UIButton){
print("i miss you ....")
}
func titleWithLikeButton(title:String,isFavorite : Bool){
titleLabel.text = title
let button = UIButton(frame: CGRect(x: titleLabel.intrinsicContentSize.width, y: 0, width: 44, height: 44))
//setup your button here
button.setTitleColor(UIColor.red, for: UIControlState.normal)
let image = UIImage(named: "heart-empty.png")
button.setImage(image, for: UIControlState.normal)
button.addTarget(self, action: #selector(hitForLike(_:)), for: UIControlEvents.touchUpInside)
//Add the button to the text label
titleLabel.addSubview(button)
}
2 个解决方案
#1
1
I think you need to enable user interaction on your label, like so:
我认为您需要在标签上启用用户互动,如下所示:
titleLabel.isUserInteractionEnabled = true
So your entire function ends up looking like this:
所以你的整个函数最终看起来像这样:
func titleWithLikeButton(title:String,isFavorite : Bool){
titleLabel.text = title
titleLabel.isUserInteractionEnabled = true //enable userinteraction
let button = UIButton(frame: CGRect(x: titleLabel.intrinsicContentSize.width, y: 0, width: 44, height: 44))
//setup your button here
button.setTitleColor(UIColor.red, for: UIControlState.normal)
let image = UIImage(named: "heart-empty.png")
button.setImage(image, for: UIControlState.normal)
button.addTarget(self, action: #selector(hitForLike(_:)), for: UIControlEvents.touchUpInside)
//Add the button to the text label
titleLabel.addSubview(button)
}
Hope that helps.
希望有所帮助。
#2
0
You have to set your cell button target in func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
, like below:
你必须在func tableView(_ tableView:UITableView,cellForRowAt indexPath:IndexPath) - > UITableViewCell中设置你的单元格按钮目标,如下所示:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableViewForPAC.dequeueReusableCell( withIdentifier: "TableViewCell", for: indexPath) as! TableViewCell
cell.yourButton.tag = indexpath.row
cell.yourButton.addTarget(self, action: #selector(hitForLike(_:)), for: UIControlEvents.touchUpInside)
return cell
}
func hitForLike(_ sender : UIButton){
print(sender.tag)
print("i miss you ....")
}
#1
1
I think you need to enable user interaction on your label, like so:
我认为您需要在标签上启用用户互动,如下所示:
titleLabel.isUserInteractionEnabled = true
So your entire function ends up looking like this:
所以你的整个函数最终看起来像这样:
func titleWithLikeButton(title:String,isFavorite : Bool){
titleLabel.text = title
titleLabel.isUserInteractionEnabled = true //enable userinteraction
let button = UIButton(frame: CGRect(x: titleLabel.intrinsicContentSize.width, y: 0, width: 44, height: 44))
//setup your button here
button.setTitleColor(UIColor.red, for: UIControlState.normal)
let image = UIImage(named: "heart-empty.png")
button.setImage(image, for: UIControlState.normal)
button.addTarget(self, action: #selector(hitForLike(_:)), for: UIControlEvents.touchUpInside)
//Add the button to the text label
titleLabel.addSubview(button)
}
Hope that helps.
希望有所帮助。
#2
0
You have to set your cell button target in func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
, like below:
你必须在func tableView(_ tableView:UITableView,cellForRowAt indexPath:IndexPath) - > UITableViewCell中设置你的单元格按钮目标,如下所示:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableViewForPAC.dequeueReusableCell( withIdentifier: "TableViewCell", for: indexPath) as! TableViewCell
cell.yourButton.tag = indexpath.row
cell.yourButton.addTarget(self, action: #selector(hitForLike(_:)), for: UIControlEvents.touchUpInside)
return cell
}
func hitForLike(_ sender : UIButton){
print(sender.tag)
print("i miss you ....")
}