简体   繁体   English

将操作添加到自定义UITableViewCell

[英]Add an action to a custom UITableViewCell

I am building a simple app with a table view filled with custom view cells and using a storyboard. 我正在构建一个简单的应用程序,其中包含填充了自定义视图单元格和使用故事板的表格视图。 I want to add a actions on the cell each time the user tap it. 我希望每次用户点击它时在单元格上添加一个动作。

So far, I tried to create an IBOutle to link my cell to my tableViewController and add the action manually in the code but each time I try to do it I get an error message saying "Illegal Configuration - cannot have a prototype object as its destination". 到目前为止,我尝试创建一个IBOutle来将我的单元格链接到我的tableViewController并在代码中手动添加动作,但每次我尝试这样做时都会收到错误消息“非法配置 - 不能将原型对象作为其目标”。

The only quick fix I found is to add a UIButton with a transparent background and no title which fills in the whole cell and attached the action to it. 我找到的唯一快速解决方案是添加一个透明背景的UIButton ,没有标题填充整个单元格并将操作附加到它。

Is there any more elegant way to do it? 还有更优雅的方式吗?

Not only is there a more "elegant" solution, but there is also a correct way to do this.You should be using the didSelectRowAtIndexPath method for your cells. 不仅有更“优雅”的解决方案,而且还有一种正确的方法。您应该为您的单元格使用didSelectRowAtIndexPath方法。

You should read what the Apple Docs have to say 您应该阅读Apple Docs所说的内容

And also, here is a tutorial on how to use row selection in your tableView. 此外, 这是一个关于如何在tableView中使用行选择的教程

优雅的解决方案是使用表视图的委托方法tableView:didSelectRowAtIndexPath:并将所有操作代码放在那里。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM