如何理解superview

Posted

技术标签:

【中文标题】如何理解superview【英文标题】:How to understand superview 【发布时间】:2014-03-19 05:53:59 【问题描述】:

我有一个 customCell 并在单元格中添加了一些按钮。

SettingTableViewCell * cell = (SettingTableViewCell *)[[sender superview] superview];
NSIndexPath * path = [self.tableView indexPathForCell:cell];

第二句怎么理解!

非常感谢!

【问题讨论】:

第二句用于获取当前单元格的indexPath (about indexPath)。 【参考方案1】:

不知道你想做什么。但我对这两行给出了解释。

[sender superview] 这意味着,你可以点击按钮,这就是为什么你会得到这个sender,它放在单元格的内容视图上。然后你再次调用superview],所以你可以得到superview of content view,它在单元格中。请参阅下面的视图层次结构。

----cell
-------ContentView
-------------Sender

NSIndexPath * path = [self.tableView indexPathForCell:cell];。此行将为保存在 tableview 单元格堆栈中的相应单元格提供 indexPath。

【讨论】:

是的,我明白了!我不明白它们之间的层级关系。谢谢! 您的单元格将内容视图作为子视图,而内容视图将按钮作为子视图。那是在视图层次结构中。请参阅此链接以了解有关视图层次结构的更多信息。 developer.apple.com/library/ios/documentation/windowsviews/… 当我customCell - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier [self addSubview:] 和 [self.contentView addSubview] 两者的区别? 你问了一些问题,我回答了。如果您还有其他问题,请单独提出。行。答案是我不知道,但你可以在苹果的文档 The content view of a UITableViewCell object is the default superview for content displayed by the cell. If you want to customize cells by simply adding additional views, you should add them to the content view so they will be positioned appropriately as the cell transitions into and out of editing mode. 中看到这个解释 developer.apple.com/library/ios/documentation/uikit/reference/…

以上是关于如何理解superview的主要内容,如果未能解决你的问题,请参考以下文章

UIView属性的讲解

iOS 视图在不同View之间的切换(对于convertRect:函数的一些理解)

如何根据superView设置UIView大小

如何在 Superview 的视图中插入视图? (iOS)

如何从 iPad 中的子视图更新 Superview?

如何在 iOS 8 上为 superview 制作 clearColor