自定义cell的方法
1.在继承于UITableViewCell的.m文件中(如MyTableViewCell.m)重写initWithStyle:reuseIdentifier:方法
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
//cell内的控件
}
return self;
}
2.在ViewController.h中添加协议,定义视图对象
@interface ViewController : UIViewController<UITableViewDelegate, UITableViewDataSource>
@property(nonatomic, strong)UITableView *tableView;
@end
3.在ViewController.m中为tableView注册cell
[self.tableView registerClass:[(Class)] forCellReuseIdentifier:(NSString)];
4.在tableView:cellForRowAtIndexPath:中使用dequeueReuseableCellWithIdentifier:forIndexPath:方法获取复用cell,无复用cell则使用所提供的class类创建cell并获取
MyTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
案例
1.创建继承于UITableViewCell的MyTableViewCell,并重写方法
//MyTableViewCell.h
@interface MyTableViewCell : UITableViewCell
@property(nonatomic, strong)UILabel *label;
@property(nonatomic, strong)UIButton *button;
@end
//MyTableViewCell.m
#import "MyTableViewCell.h"
@implementation MyTableViewCell
//重写initWithStyle:reuseIdentifier:方法
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if([self.reuseIdentifier isEqualToString:@"111"]) {
_label = [[UILabel alloc] init];
[self.contentView addSubview:_label];
_button = [UIButton buttonWithType:UIButtonTypeCustom];
[self.contentView addSubview:_button];
}
return self;
}
//控件布局
- (void)layoutSubviews {
_label.frame = CGRectMake(30, 10, 100, 50);
_button.frame = CGRectMake(100, 10, 50, 50);
}
@end
2.在ViewController.h中添加协议,定义视图对象
//ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UITableViewDelegate, UITableViewDataSource>
@property(nonatomic, strong)UITableView *tableView;
@end
3.在ViewController.m中为tableView注册cell,并获取复用cell
//ViewController.m
#import "ViewController.h"
#import "MyTableViewCell.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:self.tableView];
self.tableView.delegate = self;
self.tableView.dataSource = self;
//注册cell
[self.tableView registerClass:[MyTableViewCell class] forCellReuseIdentifier:@"111"];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 3;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 70;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//获取复用cell
MyTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"111" forIndexPath:indexPath];
if(indexPath.section == 1) {
cell.label.text = @"111";
[cell.button setImage:[UIImage imageNamed:@"1.jpeg"] forState:UIControlStateNormal];
}else {
cell.label.text = @"222";
[cell.button setImage:[UIImage imageNamed:@"2.jpeg"] forState:UIControlStateNormal];
}
return cell;
}
@end
效果: