[英]UIButton doesn't work on a custom UITableViewCell (Swift)
I'm really sorry for my english and if I make something wrong, and this is my first question here.我真的很抱歉我的英语,如果我做错了什么,这是我在这里的第一个问题。
I have a custom UITableViewCell as a view in *.xib file.我有一个自定义 UITableViewCell 作为 *.xib 文件中的视图。 with an UIImage in it and a button on that UIImage.
里面有一个 UIImage 和那个 UIImage 上的一个按钮。 I connected this button with an outlet to my CustomTableViewCell.swift class.
我将此按钮与一个插座连接到我的 CustomTableViewCell.swift 类。
@IBOutlet weak var authorImage: UIImageView!
@IBOutlet weak var authorButton: UIButton!
In the MyTableViewController.swift I've registered a nib在 MyTableViewController.swift 我注册了一个笔尖
tableView.registerNib(UINib(nibName: "CustomTableViewCell", bundle: nil), forCellReuseIdentifier: "my_cell")
And wrote an cellForRow... function like this:并编写了一个 cellForRow... 函数,如下所示:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("my_cell", forIndexPath: indexPath) as! CustomTableViewCell
cell.authorButton.addTarget(self, action: "authorButtonPressed:", forControlEvents: .TouchUpInside)
return cell
}
And, finally I have this function (which I use in addTarget...):而且,最后我有了这个函数(我在 addTarget 中使用它...):
func authorButtonPressed (sender:UIButton!) {
print("Hi!")//line1
print(sender)
}
and a breakpoint on the first line.以及第一行的断点。 But this function is never called.
但是这个函数永远不会被调用。
And I have also just understood that button isn't animated, when I tap it.我也刚刚了解到,当我点击按钮时,它没有动画。
How can I fix it or find a way to the solution?我该如何修复它或找到解决方法?
Thank you in advance先感谢您
You are using a UIView
as the root view in your nib.您正在使用
UIView
作为您笔尖中的根视图。 For a custom UITableViewCell
you need to make the root view a UITableViewCell:对于自定义
UITableViewCell
您需要将根视图设为 UITableViewCell:
So, you have to create an empty xib and than drag a UITableViewCell on it.因此,您必须创建一个空的 xib,然后在其上拖动一个 UITableViewCell。 Then start adding your subviews to the
ContentView
然后开始将您的子视图添加到
ContentView
You shouldn't add add target on this function:你不应该在这个函数上添加添加目标:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("my_cell", forIndexPath: indexPath) as! CustomTableViewCell
**cell.authorButton.addTarget(self, action: "authorButtonPressed:", forControlEvents: .TouchUpInside)**
return cell
}
You just add outlet action into your cell.您只需将插座动作添加到您的单元格中。 It will be fine.
没事的。 Like this code:
像这样的代码:
@IBAction func testPressed(sender: AnyObject) {
print("test")
}
按照 joern 解决方案,然后通过单击它来选择Table View Cell ,并确保应启用属性检查器窗格的User Interaction Enabled属性的复选标记。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.