1. 为什么要自定义UITableViewCell
UITableView是iOS开发中常用的控件之一,它可以快速地展示数据。而UITableView的Cell是用来展示具体数据的,系统默认提供了一些样式的Cell供我们选择使用,但是在某些情况下,这些默认样式的Cell不足以满足我们的需求,这时我们就需要自定义UITableViewCell了。
2. 自定义UITableViewCell的过程
2.1 创建自定义Cell类
继承UITableViewCell,并实现initWithStyle:reusedIdentifier方法
@interface CustomCell : UITableViewCell
@property (nonatomic,strong) UILabel *titleLabel; //标题
@property (nonatomic,strong) UIImageView *iconView;//图片
@end
@implementation CustomCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
//添加控件
self.titleLabel = [[UILabel alloc] init];
[self.contentView addSubview:self.titleLabel];
self.iconView = [[UIImageView alloc] init];
[self.contentView addSubview:self.iconView];
}
return self;
}
@end
在CustomCell类中添加了一个UILabel和一个UIImageView,它们将用来展示Cell中的标题和图片。
2.2 在UITableView中使用自定义Cell
2.2.1 注册Cell
在UITableView的创建和配置过程中,需要为可重用Cell注册类信息(如下),这样UITableView才知道如何使用自定义Cell类。
[self.tableView registerClass:[CustomCell class] forCellReuseIdentifier:@"customCell"];
2.2.2 使用自定义Cell
在UITableViewDataSource中返回CustomCell对象。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"customCell" forIndexPath:indexPath];
//配置cell展示的内容
cell.titleLabel.text = @"Cell标题";
cell.iconView.image = [UIImage imageNamed:@"image"];
return cell;
}
2.3 设置自定义Cell高度
如果我们的自定义Cell高度固定,可以在UITableViewDelegate中设置,如下:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 80.0;
}
如果自定义Cell高度不固定,需要在CustomCell中重写heightForRowAtIndexPath方法,如下:
- (CGFloat)heightForRowAtIndexPath:(NSIndexPath *)indexPath {
//计算Cell高度
return 80.0;
}
2.4 在Cell中添加Button响应事件
自定义Cell中添加Button,然后在UITableViewDelegate中设置Button响应事件即可。
@interface CustomCell : UITableViewCell
@property (nonatomic,strong) UILabel *titleLabel;
@property (nonatomic,strong) UIImageView *iconView;
@property (nonatomic,strong) UIButton *button;//自定义Button
@end
@implementation CustomCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
//添加控件
self.titleLabel = [[UILabel alloc] init];
[self.contentView addSubview:self.titleLabel];
self.iconView = [[UIImageView alloc] init];
[self.contentView addSubview:self.iconView];
self.button = [UIButton buttonWithType:UIButtonTypeCustom];
[self.button setTitle:@"Button" forState:UIControlStateNormal];
[self.button addTarget:self action:@selector(onButtonClicked) forControlEvents:UIControlEventTouchUpInside];//添加Button监听事件
[self.contentView addSubview:self.button];
}
return self;
}
- (void)layoutSubviews {
[super layoutSubviews];
self.titleLabel.frame = CGRectMake(10, 10, 100, 30);
self.iconView.frame = CGRectMake(120, 10, 30, 30);
self.button.frame = CGRectMake(200, 10, 50, 30);
}
- (void)onButtonClicked {
NSLog(@"Button Clicked");
}
@end
在UITableViewDelegate中设置Button响应事件:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
CustomCell *cell = [tableView cellForRowAtIndexPath:indexPath];
[cell.button sendActionsForControlEvents:UIControlEventTouchUpInside];//触发Button响应事件
}
3. 总结
自定义UITableViewCell可以让我们更灵活地定制Cell样式,提高用户体验。自定义UITableViewCell的过程大体可以分为创建Cell类、在UITableView中使用Cell、设置Cell高度、在Cell中添加Button响应事件等步骤,关键在于熟练掌握UITableView的相关API,灵活运用。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:详解ios中自定义cell,自定义UITableViewCell - Python技术站