自定义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
?
轉載于:https://www.cnblogs.com/star001/p/5211221.html
總結
- 上一篇: POJ 3281_Dining
- 下一篇: iOS-上架APP之启动页设置(新手必看