自定义cell
一.创建模型类
1.属性
@interface Model : NSObject
@property(nonatomic, copy)NSString *name;
@property(nonatomic, copy)NSString *gender;
// 记录是否被选中
@property(nonatomic, assign)BOOL isSelect;
@end
二 创建cell。引入模型的头文件
@interface MyTableViewCell : UITableViewCell
// 在此声明控件属性 是为了方便 在控制器中 能够访问到 自定义cell中的控件
@property(nonatomic, retain)UILabel *nameLabel;
@property(nonatomic, retain)UILabel *genderLabel;
@property(nonatomic, retain)UIButton *button;
// 此方法用于接收controller传进来的model
// 在此方法中 将model的值 赋值给控件
- (void)cellConfigureModel:(Model *)model;
@end
/ 自定义cell中的 初始化方法
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
// 在自定义方法中 我们需要做的事情是初始化控件 以及空间之间的布局
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self)
{
// 我们自定义cell的所有控件 都是要加载到cell的contentView上的
self.nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 100, 30)];
self.nameLabel.backgroundColor = [UIColor magentaColor];
[self.contentView addSubview:_nameLabel];
[_nameLabel release];
self.genderLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 50, 100, 30)];
self.genderLabel.backgroundColor = [UIColor cyanColor];
[self.contentView addSubview:_genderLabel];
[_genderLabel release];
self.button = [UIButton buttonWithType:UIButtonTypeSystem];
self.button.frame = CGRectMake(150, 10, 100, 30);
[self.button setTitle:@"选择" forState:UIControlStateNormal];
[self.contentView addSubview:_button];
[_button release];
}
return self;
}
// 在此方法中 将model的值 赋值给控件
- (void)cellConfigureModel:(Model *)model
{
self.nameLabel.text = model.name;
self.genderLabel.text = model.gender;
}
- (void)awakeFromNib {
// Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
@end
//ViewControll要自己写创建cell的方法,而UIViewCellControll自带方法
@interface RootViewController ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, retain)UITableView *tableView;
@property(nonatomic, retain)NSMutableArray *modelArray;
@end
static NSString *cellIndentifier = @"cell";
@implementation RootViewController
- (void)dealloc
{
[_tableView release];
[_modelArray release];
[super dealloc];
}
- (void)setModel
{
self.modelArray = [NSMutableArray array];
for (int i = 0; i < 10; i++)
{
Model *model = [[Model alloc]init];
model.name = [NSString stringWithFormat:@"哈哈%d",i];
if (i % 2 == 0)
{
model.gender = @"女";
}
else
model.gender = @"男";
[self.modelArray addObject:model];
[model release];
}
}
- (void)viewDidLoad {
[super viewDidLoad];
[self setModel];
// Do any additional setup after loading the view.
self.navigationController.navigationBar.translucent = NO;
self.navigationItem.title = @"自定义cell";
self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 375, 667 - 64) style:UITableViewStylePlain];
self.tableView.dataSource = self;
self.tableView.delegate = self;
// 注册自定义cell类 每次一自定义 就先给注册上
[self.tableView registerClass:[MyTableViewCell class] forCellReuseIdentifier:cellIndentifier];
[self.view addSubview:_tableView];
[_tableView release];
}
#pragma mark ---- 返回多少分区 ---
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
#pragma mark --- 每个分区下返回多少行 ---
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.modelArray.count;
}
#pragma mark --- 返回cell的方法 ---
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 普通的自定义cell写法
/*
static NSString *cellIndentifier = @"cell";
MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier];
if (cell == nil)
{
cell = [[[MyTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIndentifier]autorelease];
}
Model *model = self.modelArray[indexPath.row];
cell.nameLabel.text = model.name;
cell.genderLabel.text = model.gender;
return cell;
*/
// 不用判断 直接用下面的方法自定义cell注册的写法 tableView必须注册一个cell类
// 切记cell的标识要和注册时给的一样 否则会导致崩溃
MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier forIndexPath:indexPath];
Model *model = self.modelArray[indexPath.row];
// 传进来一个model 在cell类的内部进行赋值控件
[cell cellConfigureModel:model];
// cell 上的每个 button 添加触发方法
[cell.button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
// 将indexPath.row作为button的tag值
// 在button的触发方法中 我们需要要用到tag值寻找对应的model
cell.button.tag = 100 + indexPath.row;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
if (model.isSelect == YES)
{
cell.backgroundColor = [UIColor orangeColor];
[cell.button setTitle:@"取消" forState:UIControlStateNormal];
}
else
{
cell.backgroundColor = [UIColor clearColor];
[cell.button setTitle:@"选择" forState:UIControlStateNormal];
}
return cell;
}
#pragma mark --- button的触发方法 ---
- (void)buttonAction:(UIButton *)button
{
// 找到当前Model
Model *model = self.modelArray[button.tag - 100];
// 找到对应的cell
MyTableViewCell *cell =(MyTableViewCell *)button.superview.superview ;
NSString *title = [button titleForState:UIControlStateNormal];
if ([title isEqualToString:@"选择"])
{
// 将model置成选中状态
model.isSelect = YES;
cell.backgroundColor = [UIColor orangeColor];
[cell.button setTitle:@"取消" forState:UIControlStateNormal];
}
else
{
cell.backgroundColor = [UIColor clearColor];
[cell.button setTitle:@"选择" forState:UIControlStateNormal];
}
}
#pragma mark --- 返回每行的高度 ---
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 200;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end

浙公网安备 33010602011771号