UITableViewCell的使用和属性

来源:互联网 发布:巨人网络 史玉柱 编辑:程序博客网 时间:2024/06/05 01:13

UITableView的每一行都是一个UITableViewCell,通过dataSource的tableView:cellForRowAtIndexPath:方法来初始化每一行。

辅助指示视图

UITableViewCell内部有个默认的子视图:contentView,contentView是UITableViewCell所显示内容的父视图,可显示一些辅助指示视图。

辅助指示视图的作用是显示一个表示动作的图标,可以通过设置UITableViewCell的accessoryType来显示,默认是UITableViewCellAccessoryNone(不显示辅助指示视图)

设置方法:

cell.accessoryType = UITableViewCellAccessoryCheckmark;

其他值如下:

还可以通过cell的accessoryView属性来自定义辅助指示视图(比如往右边放一个开关)

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

contentView的3个默认子视图

2个是UILabel,和一个UIImageView
第一个UILabel的设置方式

cell.textLabel.text = hero.name;

第二个UILabel的设置方式

cell.detailTextLabel.text = hero.intro;

UIImageView的设置方式

cell.imageView.image = [UIImage imageNamed:hero.icon];

UITableViewCell的UITableViewCellStyle属性

UITableViewCell还有一个UITableViewCellStyle属性,用于决定使用contentView的哪些子视图,以及这些子视图在contentView中的位置
UITableViewCellStyle属性依次为:UITableViewCellStyleDefault、UITableViewCellStyleSubtitle、UITableViewCellStyleValue1、UITableViewCellStyleValue2 的视图样式

设置table的行高

设置table行高的两种方法
第一种:使用UITableView的属性设置

self.tableView.rowHeight = 60; // rowHeight的默认高度为44

第二种:使用代理的方法设置每行的行高(可以将每行设置不同的行高)

** *  使用代理的方法设置每行的高度(可以将不同的行设置为不同的高度) */-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{    return 60;}

设置cell的背景和选中背景

1.设置cell的背景属性有backgroundView和backgroundColor两种。
两者是有区别的,如下:
1)backgroundView可以设置背景颜色和背景图片,backgroundColor只可以设置背景颜色
2)backgroundView的优先级大于backgroundColor,如果同时设置了backgroundView和backgroundColor属性,则backgroundView会覆盖backgroundColor。

2.设置cell的长按背景(选中背景)的属性为selectedBackgroundView

注:为backgroundView和selectedBackgroundView属性设置背景的时候,view不用设置尺寸

// 设置背景色(背景view不用设置尺寸,backgroundView的优先级>backgroundColor)UIView *view = [[UIView alloc] init];view.backgroundColor = [UIColor redColor];cell.backgroundView = view;// 设置背景图片UIImageView *img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"buttondelete"]];cell.backgroundView = img;// 设置长按背景(选中背景)的背景色UIView *view = [[UIView alloc] init];view.backgroundColor = [UIColor greenColor];cell.selectedBackgroundView = view;
0 0
原创粉丝点击