正确地子类化UITableViewCell? [英] Subclassing UITableViewCell correctly?

查看:123
本文介绍了正确地子类化UITableViewCell?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将子视图添加到自己和/或内容视图之间有什么区别?

What is the difference between adding the subview to self and or to the content view?

添加到自己的子视图

- (id)initWithFrame:(CGRect)frame { if ((self = [super initWithFrame:frame])) {
    UIImage *img = [UIImage imageNamed:@"lol.jpg"]; 
    UIImageView *imgView = [[UIImageView alloc] initWithImage:img]; 
    [self addSubview:imgView];
    [imgView release]; 
    return self;
}

添加到contentView的子视图

Subview added to contentView

- (id)initWithFrame:(CGRect)frame { if ((self = [super initWithFrame:frame])) {
    UIImage *img = [UIImage imageNamed:@"lol.jpg"]; 
    UIImageView *imgView = [[UIImageView alloc] initWithImage:img]; 
    [self.contentView addSubview:imgView];
    [imgView release]; 
    return self;
}


推荐答案

根据 Apple docs


UITableViewCell对象的内容视图是单元格显示的内容的默认超级视图。如果您想要通过简单添加其他视图来自定义单元格,则应将其添加到内容视图中,以便在单元格转换为编辑模式和从编辑模式转换时适当放置。

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.

一般来说,当你对调整大小和定位你的内容正在处理的自动调整设置,和子类UITableViewCell,当你需要一些自定义行为等时,你添加到contentView。 Apple Table View编程指南中有一个很好的部分,介绍 customizing UITableViewCells

Generally you add to a contentView when you are comfortable with your resizing and positioning of your content being handled by the autoresize settings, and subclass UITableViewCell when you need some custom behavior and such. The Apple Table View Programming Guide has a great section on customizing UITableViewCells.

这篇关于正确地子类化UITableViewCell?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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