ios - UITableViewCell 自定义,在哪里附加 subview ?

标签 ios

当子类化 UITableViewCell 来定义一个完整的自定义单元格时要设置什么样式? UITableViewCellStyleDefault?实际上我既不需要标签也不需要 imageView,它们会被分配相同的吗?

因此,当将我的自定义 View 附加到 UITableViewCell 子类时,是添加到 self 还是添加到 self.contentView 更好?

可以在 UITableViewCell 子类的 drawRect 中做一些 quartz 绘图(如背景)吗?

谢谢

最佳答案

when subclassing UITableViewCell to define a complete custom cell what style to set? UITableViewCellStyleDefault? Actually I don't need neither the label or the imageView, would they be allocated the same?



由于您没有使用 UITableViewCell 的任何预定义的 UILabel 或 UIImageView subview ,所以这并不重要。我只用UITableViewCellStyleDefault .我不确定是否分配了 UILabels 或 UIImageView。但是,由于没有办法明确告诉 UITableViewCell 不要创建这些 subview ,所以我不会担心。即使创建了它们,您也不会使用它们,因此绘制单元格不会变慢,并且由于在滚动表格时会重复使用单元格,因此一次仅分配几个额外的对象不会影响整个表格 View 表现。

So when attaching my custom views to the UITableViewCell subclass, is it better to add to self, or to self.contentView?



所有的内容都应该放在 UITableViewCell contentView .这将确保当 UITableView 在右侧有索引或 UITableView(或 UITableViewCell)进入编辑模式时内容行为正常。

And is it ok to do some quartz drawing in drawRect in the UITableViewCell subclass (Like background) ?



推荐的方法是将背景放置在 UITableViewCell backgroundView属性(property)。所以我不建议在 UITableViewCell 子类本身中进行任何绘图。只需使用 contentView , backgroundView , 和 selectedBackgroundView特性。您可以创建一个简单的 UIView 子类并将其设置为 backgroundView UITableViewCell,如果你喜欢的话。

关于ios - UITableViewCell 自定义,在哪里附加 subview ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8873236/

相关文章:

ios - 每次 UIWindow 的 rootviewcontroller 改变时如何回调

iOS 你确定吗对话框

ios - 未捕获的异常 'NSInternalInconsistencyException',原因 : '-[__NSCFDictionary setObject:forKey:]: mutating method sent to immutable object'

ios - 动态调整单元格大小时出错

ios - 日期格式 - 仅显示日期

IOS Twitter 使用 PFLogInViewController 解析登录

ios - 运行 React-native ios 模拟器时出现间歇性错误 ("couldn' t 启动您定义的模拟器,因为模拟器已启动”)

iphone - 在应用程序运行时阻止 Iphone 中的调用

ios - Firemonkey Windows/iOS 兼容代码

iOS 其他应用中的点击/拖动事件