(十五)UITableViewCell的常见属性

时间:2023-03-09 19:21:45
(十五)UITableViewCell的常见属性

UItableViewCellStyle:

typedef NS_ENUM(NSInteger, UITableViewCellStyle) {
UITableViewCellStyleDefault, //左边imageView,右边显示textLabel
UITableViewCellStyleValue1, // 左边显示imageView,右边显示textLabel与detailTextLabel(横排)
UITableViewCellStyleValue2, // 左边显示蓝色的textLabel,右边显示黑色的detailTextLabel
UITableViewCellStyleSubtitle // 左边显示imageView,有textLabel和detailTextLabel(竖排)
};

accessoryType属性,右边的指示器(如箭头,指示用户点击后跳转)。

它是枚举类型,一共有5种。

typedef NS_ENUM(NSInteger, UITableViewCellAccessoryType) {
UITableViewCellAccessoryNone, // 无
UITableViewCellAccessoryDisclosureIndicator, // 向右的指示箭头
UITableViewCellAccessoryDetailDisclosureButton, // 对号 + 向右的指示箭头
UITableViewCellAccessoryCheckmark, // 对号
UITableViewCellAccessoryDetailButton NS_ENUM_AVAILABLE_IOS(7_0) // 带圈的i标志(iOS7.0开始支持)
};

Tip:访问资源库(Library)文件夹的方法,按住option键选择Finder的菜单可以看到灰色的library选项,点开即可。

Tip:发现Xcode报错头文件被修改,可以通过清理缓存实现,打开Library/Developer/Xcode 删除DerivedData文件夹。

如果系统自带的样式不够用,还可以自己定义样式,使用accessoryView属性,可以自己加入控件,例如加入一个开关。

Tip:经常打开的文件夹可以制作替身(alias,类似于windows快捷方式)来快速访问。

cell.accessoryView = [[UISwitch alloc] init];

Tip:有固定尺寸的控件是无法修改固定的尺寸的。

backgroundView与selectedbackgroundView可以设定普通和点选的样式。也可以使用backgroundColor设置,但是前者优先级高。

设定颜色:

UIView *bgView = [[UIView alloc] init];
bgView.backgroundColor = [UIColor grayColor];
cell.backgroundView = bgView; UIView *bgViewSelceted = [[UIView alloc] init];
bgViewSelceted.backgroundColor = [UIColor brownColor];
cell.selectedBackgroundView = bgViewSelceted;

注意一个细节,UIView必须创建两次,如果只创建一个,在对选中的背景色设置前更换backgroundColor,会使得第一个设置失效。

也可以直接设定图片,backgrounView可以接受任何类型的UIView,因此也可以接收UIImageView。

Tip:系统会自动填充,不用将背景设置尺寸。尽量使用backgroundView而不是Color。