自定义TableViewcell

  创建需要自定义的MyTableViewCell


  //创建MyTableViewCell继承自TableViewCell

@interface MyTableViewCell : UITableViewCell


//1 在.h文件里创建自定义cell里面需要的属性

//

@property(nonatomic, strong)UIImageView *MyimageView;

//标签

@property(nonatomic, strong)UILabel *TitleLabel;

@property(nonatomic, strong)UILabel *detailLabel;

//按钮

@property(nonatomic, strong)UIButton *Mybutton;

@end


//2.在.m文件里面重写MyTableViewCell的两个方法

   //重写其初始化方法

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

{

     //父类初始化

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

    if (self) {

         //属性初始化

        self.MyimageView = [[UIImageView alloc]init];

        self.Mybutton = [[UIButton alloc]init];

        self.TitleLabel = [[UILabel alloc]init];

        self.detailLabel = [[UILabel alloc]init];

        

        //把需要的属性添加到MyTableViewCell上面

        [self.contentView addSubview:self.Mybutton];

        [self.contentView addSubview:self.MyimageView];

        [self.contentView addSubview:self.TitleLabel];

        [self.contentView addSubview:self.detailLabel];

    }

    return self;

}


//重写布局方法

-(void)layoutSubviews

{    //父类布局

    [super layoutSubviews];

     //定义各个属性的位置

     //cell 高度100 左边是图 ,右边是按钮和标签

     //左边

    self.MyimageView.frame = CGRectMake(5, 5, 95, 95) ;

   //右边

    self.Mybutton.frame = CGRectMake(110, 50, 80, 45);

    self.TitleLabel.frame = CGRectMake(110, 5, 150, 40) ;

    self.detailLabel.frame = CGRectMake(290, 50, 70, 45) ;

    //添加背景色便于调整  确定后再注掉

    //绿色

    //self.MyimageView.backgroundColor = [UIColor greenColor];

    //浅蓝

    //self.Mybutton.backgroundColor = [UIColor cyanColor];

    //红色

    //self.TitleLabel.backgroundColor= [UIColor redColor];

    //蓝色

    //self.detailLabel.backgroundColor = [UIColor blueColor];

}


  上面已经完成了TableViewCell的自定义  下面是调用;



 在ViewController里面导入MyTableView头文件

#import "MyTableViewCell.h"

                               //签订TableView协议

@interface ViewController () <UITableViewDelegate, UITableViewDataSource>


//创建建表的两个属性 一个tableView

@property(nonatomic, strong)UITableView *tab;

 //一个是存放数据的数组

@property(nonatomic, strong)NSMutableArray *dataArray;

@end

@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    //初始化属性

    //初始化tableView

    _tab = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 375, 667) style:UITableViewStylePlain];

    [self.view addSubview:_tab];

    //初始化数据数组

    _dataArray = [NSMutableArray array];

    


     //给数据数组加点显示的东西

    [_dataArray addObject:@""];

    [_dataArray addObject:@""];

    [_dataArray addObject:@""];

    [_dataArray addObject:@""];

    [_dataArray addObject:@""];

    [_dataArray addObject:@""];

    [_dataArray addObject:@""];

    //设置_tab的代理

    _tab.delegate = self;

    _tab.dataSource = self;

    

    // Do any additional setup after loading the view, typically from a nib.

}

//tableView的代理方法

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

    return _dataArray.count;

}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

 static NSString *idenStr = @"cell";

    //使用标识从tableview里面获取一个cell

 MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:idenStr];

    //判断有没有

    if(cell == nil)

    {

         //没有就创建一个cell

        cell = [[MyTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:idenStr];

    }

    //给自定义的cell赋值

     //取出数据数组的数据 : 一二三.....

    NSString *str = [_dataArray objectAtIndex:indexPath.row];

  

    //随便给imageView赋一个图

    cell.MyimageView.image = [UIImage imageNamed:@"icon.png"];

    //按钮文字 标签文字都赋上str的值

                                                                                      //拼接提示

    [cell.Mybutton setTitle:[str stringByAppendingString:@".按钮"] forState:UIControlStateNormal];

    [cell.Mybutton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];         

                                   //拼接提示

    cell.TitleLabel.text = [str stringByAppendingString:@".文本1"];

                                     //拼接提示

    cell.detailLabel.text = [str stringByAppendingString:@".文本2"];

    return cell;

}

//设置tableView的行高为100,跟自定义的cell一样大;

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

{

    return 100;

}



结果如图所示





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值