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*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;
}
自定义cell方法
第一步:新建一个继承于UITableviewCell的类;
第二步:在新建的类里把你要往cell上加的控件作为属性;

@property(nonatomic,strong)UIImageView*image;
@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;
}

第四步:在原来的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;
}

点击cell上的button时弹出cell对应信息的方法
1.创建cell时,button跟其他控件一起初始化
if(cell == nil) {
        cell = [[
Myinforalloc]initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:iden];
        [cell.
btnaddTarget:selfaction:@selector(callPhone:)forControlEvents:UIControlEventTouchUpInside];//精华
    }


-(void)callPhone:(UIButton*)btn{
   
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
原创粉丝点击