UI学习 第七章 UITableView每行的cell不同的设置
来源:互联网 发布:js offsetwidth 错误 编辑:程序博客网 时间:2024/06/06 17:33
UI学习 第七章 UITableView每行的cell不同的设置
下中Info为自定义类,内有属性
@property(nonatomic,strong)UIImageView*newsImg;
@property(nonatomic,strong)UILabel*titleLabel;
@property(nonatomic,strong)UILabel*titleLabel;
@property(nonatomic,strong)UILabel*subLabel;
下中为ViewController.m文件
- (void)viewDidLoad {
[superviewDidLoad];
table = [[UITableViewalloc]initWithFrame:self.view.boundsstyle:UITableViewStylePlain];
table.delegate= self;
table.dataSource= self;
[self.viewaddSubview:table];
Info *info1 = [[Infoalloc]init];
info1.imageName= @"1";
info1.tittle= @"2";
info1.subTittle= @"3";
Info *info2 = [[Infoalloc]init];
info2.imageName= @"2.jpg";
info2.tittle= @"22";
info2.subTittle= @"32";
Info *info3 = [[Infoalloc]init];
info3.imageName= @"3.jpg";
info3.tittle= @"23";
info3.subTittle= @"33";
arr = @[info1,info2,info3];
}
-(NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section{
return arr.count;
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
static NSString *iden = @"Cell";
UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:iden];
if (cell == nil) {
cell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:iden];
}
Info *info = arr[indexPath.row];
cell.textLabel.text= info.tittle;
cell.detailTextLabel.text= info.subTittle;
cell.imageView.image= [UIImageimageNamed:info.imageName];
return cell;
table = [[UITableViewalloc]initWithFrame:self.view.boundsstyle:UITableViewStylePlain];
table.delegate= self;
table.dataSource= self;
[self.viewaddSubview:table];
Info *info1 = [[Infoalloc]init];
info1.imageName= @"1";
info1.tittle= @"2";
info1.subTittle= @"3";
Info *info2 = [[Infoalloc]init];
info2.imageName= @"2.jpg";
info2.tittle= @"22";
info2.subTittle= @"32";
Info *info3 = [[Infoalloc]init];
info3.imageName= @"3.jpg";
info3.tittle= @"23";
info3.subTittle= @"33";
arr = @[info1,info2,info3];
}
-(NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section{
return arr.count;
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
static NSString *iden = @"Cell";
UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:iden];
if (cell == nil) {
cell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:iden];
}
Info *info = arr[indexPath.row];
cell.textLabel.text= info.tittle;
cell.detailTextLabel.text= info.subTittle;
cell.imageView.image= [UIImageimageNamed:info.imageName];
return cell;
}
自定义cell方法
第一步:新建一个继承于UITableviewCell的类;
第二步:在新建的类里把你要往cell上加的控件作为属性;
@property(nonatomic,strong)UIImageView*image;
@property(nonatomic,strong)UILabel*tittle;
@property(nonatomic,strong)UILabel*tittle;
@property(nonatomic,strong)UILabel*subTittle;
第三步:在新建的类的.m文件里把往cell上加的控件初始化;
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString*)reuseIdentifier{
self = [superinitWithStyle:stylereuseIdentifier:reuseIdentifier];
if (self) {
_image = [[UIImageViewalloc]initWithFrame:CGRectMake(10,10,20,20)];
[selfaddSubview:_image];
_tittle = [[UILabelalloc]initWithFrame:CGRectMake(35,10,40,20)];
[selfaddSubview:_tittle];
_subTittle = [[UILabelalloc]initWithFrame:CGRectMake(35,20,40,20)];
[selfaddSubview:_subTittle];
}
return self;
self = [superinitWithStyle:stylereuseIdentifier:reuseIdentifier];
if (self) {
_image = [[UIImageViewalloc]initWithFrame:CGRectMake(10,10,20,20)];
[selfaddSubview:_image];
_tittle = [[UILabelalloc]initWithFrame:CGRectMake(35,10,40,20)];
[selfaddSubview:_tittle];
_subTittle = [[UILabelalloc]initWithFrame:CGRectMake(35,20,40,20)];
[selfaddSubview:_subTittle];
}
return self;
}
第四步:在原来的cell里把原来的系统类型改为自定义类的类型
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
static NSString *iden = @"Cell";
MyInfor *cell = [tableViewdequeueReusableCellWithIdentifier:iden];
if (cell == nil) {
cell = [[MyInforalloc]initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:iden];
}
Info *info = arr[indexPath.row];
cell.tittle.text= info.tittle;
cell.subTittle.text= info.subTittle;
cell.image.image= [UIImageimageNamed:info.imageName];
return cell;
static NSString *iden = @"Cell";
MyInfor *cell = [tableViewdequeueReusableCellWithIdentifier:iden];
if (cell == nil) {
cell = [[MyInforalloc]initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:iden];
}
Info *info = arr[indexPath.row];
cell.tittle.text= info.tittle;
cell.subTittle.text= info.subTittle;
cell.image.image= [UIImageimageNamed:info.imageName];
return cell;
}
点击cell上的button时弹出cell对应信息的方法
1.创建cell时,button跟其他控件一起初始化
if(cell == nil) {
cell = [[Myinforalloc]initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:iden];
[cell.btnaddTarget:selfaction:@selector(callPhone:)forControlEvents:UIControlEventTouchUpInside];//精华
cell = [[Myinforalloc]initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:iden];
[cell.btnaddTarget:selfaction:@selector(callPhone:)forControlEvents:UIControlEventTouchUpInside];//精华
}
-(void)callPhone:(UIButton*)btn{
Infor *info = arr[btn.tag];
Infor *info = arr[btn.tag];
NSLog(@"%@",info.tel);
}//精华
// 用自定义cell 可以充分任意利用一个cell 长条,为其添加控件,并为控件分配空间。如果不使用自定义cell,那么可以对cell操作的只有添加图片 cell.imageView.image 、显示标题 cell.TextLabel.text 、显示详细内容 cell.detailText.text、(并只有四种显示Style: Default subtitle value1 value2)等小东东,
0 0
- UI学习 第七章 UITableView每行的cell不同的设置
- IOS 自定义每行高度不同的UITableView
- UITableView的cell一些设置
- UITableView不同样式cell的封装
- 获取UITableView每行中不同的UITextField输入的内容
- UITableView中点击不同Cell跳转到不同的View
- 怎么设置UITableView Cell之间的距离
- 设置UITableView cell的左边距
- 设置UITableView的cell能否被选中
- UITableView的每行的缩进
- UITableView绑定不同的cell 的 ID 写法
- UItableview一个自定义cell和不同高度的例子
- 设置表格每行的单元格的过滤条件都不同
- UITableView的Cell排序
- UITableView cell的重用
- UITableView cell的重复使用
- UITableView 的 自定义cell
- UITableView的自定义cell
- 修改基于formView的MFC单文档背景颜色
- 二分图的匈牙利和KM算法感受
- 电话号码对应英语单词
- Java NIO教程(中)
- java面试题
- UI学习 第七章 UITableView每行的cell不同的设置
- spring security 安全框架remember me,demo学习
- 微信网页中获取用户基本信息(网页授权操作)
- win8.1 文件读写(二)
- 字符串的charAt()方法
- JAVA中的基础语句
- 计算字符串的相似度
- 二叉树构造算法的验证
- UI学习 第八章 导航控制器