日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

自定义cell

發(fā)布時(shí)間:2025/4/9 编程问答 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 自定义cell 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

.創(chuàng)建模型類

1.屬性

@interface Model : NSObject

@property(nonatomic, copy)NSString *name;

@property(nonatomic, copy)NSString *gender;

// 記錄是否被選中

@property(nonatomic, assign)BOOL isSelect;

@end

?

? 創(chuàng)建cell。引入模型的頭文件

@interface MyTableViewCell : UITableViewCell

// 在此聲明控件屬性 是為了方便 在控制器中 能夠訪問到 自定義cell中的控件

@property(nonatomic, retain)UILabel *nameLabel;

@property(nonatomic, retain)UILabel *genderLabel;

@property(nonatomic, retain)UIButton *button;

// 此方法用于接收controller傳進(jìn)來的model

// 在此方法中 model的值 賦值給控件

- (void)cellConfigureModel:(Model *)model;

@end

?

?

/ 自定義cell中的 初始化方法

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier

{

? ? // 在自定義方法中 我們需要做的事情是初始化控件 以及空間之間的布局

? ? self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];

? ? if (self)

? ? {

? ? ? ? // 我們自定義cell的所有控件 都是要加載到cellcontentView上的

? ? ? ? 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要自己寫創(chuàng)建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;

?? ?

? ? // 注冊(cè)自定義cell 每次一自定義 就先給注冊(cè)上

? ? [self.tableView registerClass:[MyTableViewCell class] forCellReuseIdentifier:cellIndentifier];

?? ?

? ? [self.view addSubview:_tableView];

? ? [_tableView release];

}

?

#pragma mark ---- 返回多少分區(qū) ---

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

? ? return 1;

}

#pragma mark --- 每個(gè)分區(qū)下返回多少行 ---

- (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注冊(cè)的寫法 tableView必須注冊(cè)一個(gè)cell

? ? // 切記cell的標(biāo)識(shí)要和注冊(cè)時(shí)給的一樣 否則會(huì)導(dǎo)致崩潰

? ? MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier forIndexPath:indexPath];

? ? Model *model = self.modelArray[indexPath.row];

? ? // 傳進(jìn)來一個(gè)model cell類的內(nèi)部進(jìn)行賦值控件

? ? [cell cellConfigureModel:model];

?? ?

? ? // cell 上的每個(gè) button 添加觸發(fā)方法

? ? [cell.button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];

? ? // indexPath.row作為buttontag

? ? // button的觸發(fā)方法中 我們需要要用到tag值尋找對(duì)應(yīng)的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的觸發(fā)方法 ---

- (void)buttonAction:(UIButton *)button

{

? ? // 找到當(dāng)前Model

? ? Model *model = self.modelArray[button.tag - 100];

? ? // 找到對(duì)應(yīng)的cell

? ? MyTableViewCell *cell =(MyTableViewCell *)button.superview.superview? ? ? ;

? ? NSString *title = [button titleForState:UIControlStateNormal];

? ? if ([title isEqualToString:@"選擇"])

? ? {

? ? ? ? // model置成選中狀態(tài)

? ? ? ? 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

?

轉(zhuǎn)載于:https://www.cnblogs.com/star001/p/5211221.html

總結(jié)

以上是生活随笔為你收集整理的自定义cell的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。