为什么要自定义cell:系统提供的cell满足不了复杂的样式,因此:自定义Cell和自定义视图一样,自己创建一种符合我们需求的Cell并使用这个Cell.
- 非注册
-(id)dequeueReusableCellWithIdentifier:(NSString *)identifier;
如果没有复用cell,程序可能会返回nil, 所以创建完cell后必须要做判空处理,未获取到则重新创建。这种方法可以不用注册。
//尝试获得可以复用的单元格。但不一定获得到。得不到返回nil
UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:@"mycell"];
if(cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"mycell"];
}
- 注册
(id)dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath;
获取复用的cell,如果没有复用的cell,将自动使用提供的class类创建新的cell并返回,后面不需要再进行判空。在此之前必须要注册cell。
[self.tableView registerClass:[MyTableViewCell class] forCellReuseIdentifier:@"mycell"];
MyTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"mycell" forIndexPath:indexPath];
- 区别
代码上的区别在于,注册的方法需要提前将要复用的 Cell 类注册,而不需要在获取 Cell 时手动判断 dequeue 的结果是否为nil,这是因为 dequeueReusableCellWithIdentifier:identifier forIndexPath:在内部处理了这个过程,使得最后返回的一定是可用的 Cell.
代码自定义cell:
建立继承UITableViewCell的MyTableViewCell文件,为该类添加我们需要的属性
// MyTableViewCell.h文件
@interface MyTableViewCell : UITableViewCell
@property UILabel *label;
@property UIButton *button;
@end
重写- (instancetype) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier方法
& - (void) layoutSubviews方法
#import "MyTableViewCell.h"
@implementation MyTableViewCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if ([self.reuseIdentifier isEqualToString:@"mycell"]) {
_label = [[UILabel alloc] init];
[self.contentView addSubview:_label];
_button = [UIButton buttonWithType:UIButtonTypeCustom];
[self.contentView addSubview:_btn];
}
return self;
}
//布局
- (void)layoutSubviews {
_label.frame = CGRectMake(20, 20, 100, 50);
_button.frame = CGRectMake(20, 80, 50, 50);
}
@end
.h文件里加入协议
@interface ViewController : UIViewController <UITableViewDelegate,UITableViewDataSource>
@property(nonatomic, strong) UITableView *tableView;
#import "ViewController.h"
#import "MyTableViewCell.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor whiteColor];
self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStyleGrouped];
[self.view addSubview:self.tableView];
//设置代理对象
self.tableView.delegate = self;
self.tableView.dataSource = self;
[self.tableView registerClass:[MyTableViewCell class] forCellReuseIdentifier:@"mycell"];
}
//组数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 2;
}
//组内行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 4;
}
//单元格高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 80;
}
//设置单元格
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
MyTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"mycell" forIndexPath:indexPath];
if(indexPath.section == 0) {
cell.label.text = @"Leo";
[cell.btn setImage:[UIImage imageNamed:@"1.jpg"] forState:UIControlStateNormal];
} else {
cell.label.text = @"Vincent";
[cell.btn setImage:[UIImage imageNamed:@"2.jpg"] forState:UIControlStateNormal];
}
return cell;
}
@end