将操作添加到自定义UITableViewCell [英] Add an action to a custom UITableViewCell

查看:80
本文介绍了将操作添加到自定义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.

到目前为止,我试图创建一个 IBOutle 将我的单元格链接到我的 tableViewController 并在代码中手动添加动作,但每次我尝试这样做时都会收到错误消息非法配置 - 不能有一个原型对象作为其目的地。

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".

我找到的唯一快速解决方案是添加一个 UIButton 透明背景,没有标题填写整个单元格并附加动作。

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.

还有更优雅的方法吗?

推荐答案

不仅有更优雅的解决方案,而且还有一种正确的方法。你应该使用 didSelectRowAtIndexPath 您单元格的方法。

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.

您应该阅读 Apple Docs必须说

此外,这里是一个关于如何在tableView中使用行选择的教程

这篇关于将操作添加到自定义UITableViewCell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆