ios - 调用 indexPathForCell 时崩溃

标签 ios exception uitableview

<分区>

我有一个带有自定义单元格的 UITableView。在每个 UITableViewCell 中,都有一个 UIButton。我试图找出按钮在被点击时位于哪个单元格中。为此,我这样做了:

- (IBAction)likeTap:(id)sender {


UIButton *senderButton = (UIButton *)sender;
UITableViewCell *buttonCell = (UITableViewCell *)[senderButton superview];
UITableView* table = (UITableView *)[buttonCell superview];
NSIndexPath *pathOfTheCell = [table indexPathForCell:buttonCell];
NSInteger rowOfTheCell = [pathOfTheCell row];
NSLog(@"rowofthecell %d", rowOfTheCell);

我认为这会很好,但是当调用 indexPathForCell 时,会抛出异常。

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITableViewCell indexPathForCell:]: unrecognized selector sent to instance 0x756d650'

关于我做错了什么有什么想法吗? 谢谢!

最佳答案

这是你的问题:

(UITableViewCell *)[senderButton superview]

应该是:

(UITableViewCell *)[[senderButton superview] superview]

因为button的superview不是cell,contentView是cell的哪个subview。

关于ios - 调用 indexPathForCell 时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17387924/

相关文章:

ios - iTunes Connect (Testflight) 预发布测试已经结束。该应用程序转到 App Store。能和beta一样的版本号吗?

ios - 在 iOS 7 中切换 View 时搜索栏部分消失

ios - NSPredicate execureFetchRequest 不工作 - 可能是由于字符串中的空格

python subprocess.check_output 在某些命令上返回 CalledProcessError

ios - UITableViewCell 中的动画立即完成

android - 应用程序为什么或何时需要服务器?

Python 请求 : ignore exceptions and errors while connecting to proxy server

iphone - 普通 UIViewController 中的 UITableViewController 功能

iOS 调整 UITableheaderView 的大小

ios - iOS的xcode中的异常断点,异常可以原谅吗?