首页 > UITableViewCell 的添加子视图疑问?

UITableViewCell 的添加子视图疑问?

是使用[self addsubView] 还是[self.contentView ] 两者的区别是什么?


印象里有点区别,view是Cell继承自uiview的view,content view是官方cell定义的专门用来放内容的view,平时使用没什么区别,只是复杂情况下可能会产生一些莫名其妙的bug。


同上所说,放在contentView上好。确实是有一些区别的,比如 cell 进入编辑模式的时候(比如删除、移动),contentView会调整,而 view 就不会调整。

即使你不用到编辑模式,我也建议按照官方规范放在contentView上。我以前就在这上面差点出一个大bug,跟你分享一下我的教训。


官方文档上是要求自定义的时候用self.contentView

If you want to go beyond the predefined styles, you can add subviews to the contentView property of the cell. When adding subviews, you are responsible for positioning those views and setting their content yourself.

contentView是用来放所有cell上面要显示的内容的,是官方推荐的规范。
其它区别就不清楚了。

ps:我用的时候都是直接[self addSubview]; 没有出现什么问题。


更新 THX alladinaian

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.

在官方文档的contentview下面有上面这一句:"在content view上加subviews 才能在cell的transitions效果里面适当地移动(positioned)".


self.contentView
尽管不知道为什么,先入为主了
刚才看了一下UITableViewCell继承自UIView,而且我刚才试了一下,用self也确实可以。。。

【热门文章】
【热门文章】