之前在刚开始使用表的时候就说表能展示很多的列表信息,是要依托于cell来展示的,但是大部分时候系统的cell又不能够满足我们的需求,所以我们要自定义单元格cell,这里我介绍三种自定义单元格的方法
第一种
第一种就是简单你的自定义,在创建cell的时候将我们所需的控件添加到cell上即可
static NSString *cellId = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
if (!cell){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
// 自定义单元格-1
// 创建子视图的对象 要写在if语句内 这样可以重用
// 如果写在if括号外的话 有多少行就会被创建多少次
// 如果把设置属性写在里面,会被重用掉,所以设置属性要写在if语句外
UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(0, 20, 100, 60)];
lab.tag = 100;
lab.backgroundColor = [UIColor yellowColor];
[cell addSubview:lab];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeContactAdd];
btn.tag = 200;
btn.frame = CGRectMake(200, 0, 40, 40);
btn.backgroundColor = [UIColor redColor];
[cell addSubview:btn];
}
// 根据tag值获取对象 来赋值
UILabel *lab = (UILabel *)[cell viewWithTag:100];
lab.text = [NSString stringWithFormat:@"%ld",indexPath.row];
第二种
第二种是再创建一个继承于UITableViewCell的类,在这个类上做一些控件的操作
@interface CustomTableCell1 : UITableViewCell
//在自定义类声明在初始化方法中创建的那些对象类型
@property UILabel *nameLab;
@property UIImageView *headerImageView;
@property UILabel *timeLab;
@property UILabel *saySayLab;
@property UIButton *zanBtn;
@end
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.headerImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 20, 50, 50)];
// 隐藏边界
self.headerImageView.layer.masksToBounds = YES;
self.headerImageView.layer.cornerRadius = 25;
self.headerImageView.backgroundColor = [UIColor redColor];
[self addSubview:self.headerImageView];
self.nameLab = [[UILabel alloc] initWithFrame:CGRectMake(60, 10, 100, 40)];
[self addSubview:self.nameLab];
self.timeLab = [[UILabel alloc] initWithFrame:CGRectMake(60, 50, 100, 20)];
self.timeLab.textColor = [UIColor grayColor];
// self.timeLab.backgroundColor = [UIColor redColor];
[self addSubview:self.timeLab];
self.saySayLab = [[UILabel alloc] initWithFrame:CGRectMake(10, 80, 320, 50)];
self.saySayLab.backgroundColor = [UIColor yellowColor];
self.saySayLab.numberOfLines = 0;
[self addSubview:self.saySayLab];
_zanBtn = [UIButton buttonWithType:UIButtonTypeContactAdd];
_zanBtn.frame = CGRectMake(300, 20, 20, 20);
[self addSubview:_zanBtn];
}
return self;
}
这样使用就好了
CustomTableCell1 *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
if (!cell){
cell = [[CustomTableCell1 alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
}
// 因为CustomTableCell1 是继承UITableViewCell 的所有的单元格显示的子控件 都写在了自定义类的初始化方法中,改变对象的属性 如果写在初始化方法中,所有的单元格都会显示相同的内容,so 我们需要在配置单元格内来改变子控件的属性
// 获取自定义类中的子控件的对象
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.headerImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"sx%ld",indexPath.row]];
cell.nameLab.text = [_nameArray objectAtIndex:indexPath.row];
cell.timeLab.text = @"今天 10:20";
cell.saySayLab.text = @"今天是个好日子,到处阳光明媚,鸟语花香,真的真的真的好开心啊";
// 既然可以获取到对象绑定方法就写在viewcontoller中就行了
[cell.zanBtn addTarget:self action:@selector(zanClick:) forControlEvents:UIControlEventTouchUpInside];
第三种
第三种与第二种类似,只不过第三种是使用的xib创建的,在使用的时候最好使用注册的方法来使用
// 创建nib对象
UINib *nib = [UINib nibWithNibName:@"CustomTableViewCell" bundle:nil];
[_tableView registerNib:nib forCellReuseIdentifier:@"cell"];
#warning 注意以下重要的问题
// 1.registerNib registerClass 方法 这个两个一起使用dequeueReusableCellWithIdentifier: forIndexPath: 方法
// 2.记得要把子视图 关联成属性
// 3.使用xib 改变了行高之后,一定要去修改表的默认行高
然后再在cell创建的地方使用
CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId forIndexPath:indexPath];
NSLog(@" === cell %@",cell);
cell.headerImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"sx%ld",indexPath.row]];
cell.nameLab.text = @"hello";
cell.timeLab.text = @"好开心啊";
在使用自定义cell的时候要注意的是 cell 的重用机制引发的一些问题,这一点要需要实际的操作遇到了才会知道,赶快试试吧~