自定义cell

第一步:
AppDelegate.m
首先导入头文件 #import “ViewController.h”

self.window.rootViewController = [[UINavigationController alloc]initWithRootViewController:[ViewController new]];

第二步:
ViewController.m

<UITableViewDelegate,UITableViewDataSource>

接下来viewDidLoad里

// 导航条 50 111 171
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:50/256.0 green:111/256.0 blue:171/256.0 alpha:1.0];
    UIImageView *imgV = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 150, 40)];
    imgV.image = [UIImage imageNamed:@"1"];
    
    self.navigationItem.titleView =imgV;
    
    // 右边按钮
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"2"] style:UIBarButtonItemStyleDone target:self action:nil];
    
    
    // 表格
    // 1.frame
    UITableView *tbv = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
    // 2.数据源代理
    tbv.delegate = self;
    tbv.dataSource = self;
    // 行高
    tbv.rowHeight = 100;
    // 3.添加
    [self.view addSubview:tbv];
    

}

// 几行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    return 3;
    
    
}


// 每行有啥
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *str = @"11";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
    if(cell == nil){
        
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:str];

    }
    
    // 图片
    NSArray *imgArr = @[@"3",@"4",@"5"];
    UIImageView *imgV = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 120, 80)];
//    imgV.backgroundColor = [UIColor redColor];
    imgV.image = [UIImage imageNamed:imgArr[indexPath.row]];
    [cell addSubview:imgV];
    
    // 主标题
    NSArray *zArr = @[@"科目三电子考要来了",@"驾考新规要来了,你准备好了吗",@"小伙伴,要不要体验一下职能教学"];
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(150, 12, 350, 30)];
    label.text =  zArr[indexPath.row];
    label.font = [UIFont systemFontOfSize:15];
    [cell addSubview:label];
    
    // 按钮
    UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(150, 60, 60, 26)];
    
    [btn setTitle:@"驾校公告" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:13];
    btn.layer.borderColor = [UIColor orangeColor].CGColor;
    btn.layer.borderWidth = 1;
    btn.layer.masksToBounds = YES;
    btn.layer.cornerRadius = 4;
    
    [cell addSubview:btn];
    
    // 主标题
    NSArray *Arr = @[@"8天前",@"1分前",@"2小时前"];
    UILabel *tlabel = [[UILabel alloc]initWithFrame:CGRectMake(350  , 60, 60, 25)];
    tlabel.text =  Arr[indexPath.row];
    tlabel.font = [UIFont systemFontOfSize:15];
    tlabel.textAlignment = NSTextAlignmentCenter;
    tlabel.textColor = [UIColor lightGrayColor];
    [cell addSubview:tlabel];
    
    
    
    
    
    
    return cell;
    
}

运行效果:
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/Py88888/article/details/83305935