当我单击某个按钮时,如何找到该按钮所在的 UITableviewCell 的索引路径? [英] How can I locate the indexpath of the UITableviewCell a certain button is in when I click that button?

查看:19
本文介绍了当我单击某个按钮时,如何找到该按钮所在的 UITableviewCell 的索引路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我添加了一个按钮作为 uitableviewcell 的附件视图,当我按下它时,我希望能够访问它当前所在的表视图单元格的索引路径,以便我可以删除/修改该单元格的内容.我应该继承 UIbutton 并添加使其拥有自己的索引路径属性吗?如果是这样,我是否需要在该子类中实现任何特定的按钮方法,它们会自动加载吗?任何帮助将不胜感激.对不起,如果这是一个菜鸟问题.

I've added a button as an accessory view to uitableviewcell, and I when I press it, I'd like to be able to access the index path of the table view cell it's currently inside of so I can delete/modify the contents of that cell. Should I subclass UIbutton and add make it have it's own index path property? If so, do I need to implement any specific button methods in that subclass of will they automatically be loaded? Any help would be greatly appreciated. Sorry if this is a noobie question.

推荐答案

您的子类化解决方案很好,但仅适用于自定义按钮.[UIButton buttonWithType:] 允许返回私有子类,您不应该将其子类化.除了合成 indexPath 属性之外,您不需要实现任何方法.

Your solution of subclassing is good, but only works with custom buttons. [UIButton buttonWithType:] is allowed to return private subclasses, which you are not supposed to subclass. You would not need to implement any methods beyond synthesizing the indexPath property.

一个不优雅但实用的解决方案是从按钮向上走超级视图,直到视图成为 UITableViewCell,然后向表格视图询问该单元格的索引路径.

An inelegant but functional solution would be to walk up superviews from the button until the view is a UITableViewCell, then ask the table view for the index path of that cell.

如果您不使用部分,则可以将单元格的行滑入按钮标记中.不健壮或优雅,但它有效.

If you are not using sections, you could slip the row of the cell into the button tag. Not robust or elegant, but it works.

您可以创建 UITableViewCell 的自定义子类并将其用作按钮的操作目标.然后它会调用您当前的操作目标,将自己作为发送者传递.

You could make a custom subclass of UITableViewCell and use it as the action target for the button. It would then call through to your current action target passing itself as the sender.

这篇关于当我单击某个按钮时,如何找到该按钮所在的 UITableviewCell 的索引路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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