自定义tableViewCell和设置多选
2015-09-01 18:20
337 查看
TableView.m 头文件
TableView.h 头文件
自定义cell.m 文件
自定义cell.h 文件
效果图如下:
![](https://img-blog.csdn.net/20150901181550117?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
另外注意的点:
1、dequeueReuseableCellWithIdentifier:与dequeueReuseableCellWithIdentifier:forIndexPath:的区别:
前者不必向tableView注册cell的Identifier,但需要判断获取的cell是否为nil;
后者则必须向table注册cell,可省略判断获取的cell是否为空,因为无可复用cell时runtime将使用注册时提供的资源去新建一个cell并返回
2、自定义cell时,记得将其他内容加到self.contentView 上,而不是直接添加到 cell 本身上
3、自定义cell时,若使用nib,使用 registerNib: 注册,dequeue时会调用 cell 的 -(void)awakeFromNib
不使用nib,使用 registerClass: 注册, dequeue时会调用 cell 的 - (id)initWithStyle:withReuseableCellIdentifier:
4、使用dequeueReuseableCellWithIdentifier:可不注册,但是必须对获取回来的cell进行判断是否为空,若空则手动创建新的cell;使用dequeueReuseableCellWithIdentifier:forIndexPath:必须注册,但返回的cell可省略空值判断的步骤。
#import <UIKit/UIKit.h> @interface SmallKindsTableVC : UITableViewController -(void)setNavigatonControllerTitle:(NSString*)title; @end
TableView.h 头文件
#import "SmallKindsTableVC.h" #import "SmallKindTableViewCell.h" #import "DataSource.h" @interface SmallKindsTableVC () @property (strong, nonatomic) NSString* bigKindName; @property (strong, nonatomic) NSArray* contents; @property (strong, nonatomic) NSArray* details; @end @implementation SmallKindsTableVC - (void)viewDidLoad { [super viewDidLoad]; //添加工具栏 UIWindow* window = [UIApplication sharedApplication].keyWindow; UIView* view = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetHeight(window.bounds)-55.0f, CGRectGetWidth(window.bounds), 55.0f)]; view.backgroundColor = [UIColor colorWithRed:214.0f/255.0f green:214.0f/255.0f blue:214.0f/255.0f alpha:1.0f]; UIButton* submitBtn = [UIButton buttonWithType:UIButtonTypeCustom]; submitBtn.backgroundColor = [UIColor colorWithRed:62.0f/255.0f green:204.0f/255.0f blue:153.0f/255.0f alpha:1.0f]; submitBtn.frame = CGRectMake(45, 10, CGRectGetWidth(view.frame)-90, CGRectGetHeight(view.frame)-20); //设置button内容时,必须要调用它的方法赋值. // submitBtn.titleLabel.text=@"确定"; 调它的属性是没起作用的。 [submitBtn setTitle:@"确定" forState:UIControlStateNormal]; submitBtn.titleLabel.textColor = [UIColor blackColor]; submitBtn.titleLabel.font = [UIFont systemFontOfSize:16.0f]; [submitBtn addTarget:self action:@selector(submitBtnEvent:) forControlEvents:UIControlEventTouchUpInside]; [view addSubview:submitBtn]; [window addSubview:view]; //设置tableview多选 self.tableView.allowsMultipleSelectionDuringEditing = YES; [self.tableView setEditing:YES];//设置UITableView为编译状态 [self.tableView setSeparatorColor:[UIColor lightGrayColor]];//设置tableViewCell间的分割线的颜色 //设置一个属性保存我当前选择的indexPath集合 NSArray* indexPaths=[self.tableView indexPathsForSelectedRows]; //读取数据 DataSource* dataSource = [DataSource shareDataSource]; self.contents = [[NSArray alloc]initWithArray:[dataSource getSmallKindContentsWithBigKind:self.bigKindName]]; self.details = [[NSArray alloc]initWithArray:[dataSource getSmallKindDetailsWithBigKind:self.bigKindName]]; } -(void)setNavigatonControllerTitle:(NSString*)title { self.bigKindName = title; self.navigationItem.title = title; } #pragma mark - Table view data source - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [self.contents count]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSString* identifier=[NSString stringWithFormat:@"%ld",indexPath.row]; SmallKindTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier]; if (!cell) { cell = [[SmallKindTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier]; // 自定义UITableViewCell选中时背景 UIView* view = [[UIView alloc]initWithFrame:cell.frame]; view.backgroundColor=[UIColor whiteColor]; cell.selectedBackgroundView = view; } [cell setContentLabelText:self.contents[indexPath.row] detailLabelText:self.details[indexPath.row]]; return cell; } -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 44.0f; }
自定义cell.m 文件
#import <UIKit/UIKit.h> @interface SmallKindTableViewCell : UITableViewCell -(void)setContentLabelText:(NSString *)contentText detailLabelText:(NSString *)detailText; @end
自定义cell.h 文件
#import "SmallKindTableViewCell.h" @implementation SmallKindTableViewCell { UILabel* contentLabel; UILabel* detailLabel; } - (void)setSelected:(BOOL)selected animated:(BOOL)animated { [super setSelected:selected animated:animated]; // Configure the view for the selected state } -(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { self=[super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { // [contentLabel sizeToFit];//自动设置label的高 // 初始化label contentLabel = [[UILabel alloc]init]; contentLabel.textAlignment = NSTextAlignmentNatural; contentLabel.text = @"contentText";//给初始文本 contentLabel.textColor = [UIColor blackColor]; contentLabel.font = [UIFont systemFontOfSize:16.0f];//设置字体 [contentLabel setNumberOfLines:1];//设置行数 detailLabel = [[UILabel alloc]init]; detailLabel.text = @"deatailText"; detailLabel.textColor = [UIColor lightGrayColor]; detailLabel.textAlignment = NSTextAlignmentNatural; detailLabel.font = [UIFont systemFontOfSize:14.0f]; [detailLabel setNumberOfLines:1]; } return self; } -(void)setContentLabelText:(NSString *)contentText detailLabelText:(NSString *)detailText { contentLabel.text = contentText; detailLabel.text = detailText; //设置label根据自身内容调整对应的宽度和高度,最开始是不能给label的frame的 //设置时,必须是在给定label内容时才设置添加的 //获取字符串的高度和宽度 CGSize contentLabelSize = [contentLabel.text sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16.0f]}]; contentLabel.frame = CGRectMake(0, 0, contentLabelSize.width, CGRectGetHeight(self.contentView.frame)); [self.contentView addSubview:contentLabel]; CGSize detailLabelSize = [detailLabel.text sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14.0f]}]; detailLabel.frame=CGRectMake(CGRectGetWidth(contentLabel.frame), 0, detailLabelSize.width, CGRectGetHeight(self.contentView.frame)); [self.contentView addSubview:detailLabel]; } @end
效果图如下:
另外注意的点:
1、dequeueReuseableCellWithIdentifier:与dequeueReuseableCellWithIdentifier:forIndexPath:的区别:
前者不必向tableView注册cell的Identifier,但需要判断获取的cell是否为nil;
后者则必须向table注册cell,可省略判断获取的cell是否为空,因为无可复用cell时runtime将使用注册时提供的资源去新建一个cell并返回
2、自定义cell时,记得将其他内容加到self.contentView 上,而不是直接添加到 cell 本身上
3、自定义cell时,若使用nib,使用 registerNib: 注册,dequeue时会调用 cell 的 -(void)awakeFromNib
不使用nib,使用 registerClass: 注册, dequeue时会调用 cell 的 - (id)initWithStyle:withReuseableCellIdentifier:
4、使用dequeueReuseableCellWithIdentifier:可不注册,但是必须对获取回来的cell进行判断是否为空,若空则手动创建新的cell;使用dequeueReuseableCellWithIdentifier:forIndexPath:必须注册,但返回的cell可省略空值判断的步骤。
相关文章推荐
- Android自定义TextView链接点击和长按事件
- 隐式动画
- Internet of Lights and Switches(MAP记录+二分) 2015年湖南省赛第 I 题
- bzoj1027 [JSOI2007]合金
- ExtJS4学习--多表头Grid
- LoadRunner变量到参数的互换
- Hadoop运行wordcount出现“JobTracker is not yet RUNNING”
- ssh登陆慢的问题
- 十一届蓝狐杯的总结,顺带一点解题报告
- Golang开发环境LiteIDE的设置
- GitHub和SourceTree入门教程
- 打印九九乘法表
- webservice随记
- Java初学者都必须理解的六大问题
- 高质量 Android 开发框架 LoonAndroid 详解
- 移动端Retina屏实现1px的边框
- easyui 带复选功能的下拉菜单
- 云计算设计模式(二十四)——仆人键模式
- LoadRunner lr_eval_string() 函数使用及LR中变量、参数的简单使用
- cocos2dlua-图层