IOS tableview的cell里添加UIView

#import "ViewController.h"
#define SCREEN_WIDTH     [UIScreen mainScreen].bounds.size.width  //屏幕宽
#define SCREEN_HEIGHT    [UIScreen mainScreen].bounds.size.height //屏幕高
#define BUTTOMBTN_H      50 //底部按钮高度
//实现UITableViewDelegate,UITableViewDataSource这两个协议
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    //定义3个label显示内容
    UILabel *label1;
    UILabel *label2;
    UILabel *label3;
    //定义3个按钮
    UIButton *btn1;
    UIButton *btn2;
    UIButton *btn3;
}
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) UIView *viewincell;
//定义3个数组存放数据
@property (nonatomic,  copy) NSArray *ary1;
@property (nonatomic,  copy) NSArray *ary2;
@property (nonatomic,  copy) NSArray *ary3;
@end
@implementation AddAddressViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    //设置数据
    _ary1 = @[@"开心",@"12345678901",@"我住在宇宙中的银河系中的太阳系里八大行星的地球上亚洲"];
    _ary2 = @[@"幸福",@"12345678902",@"我住在宇宙中的银河系中的太阳系里八大行星的地球上亚洲中国"];
    _ary3 = @[@"高兴",@"12345678903",@"我住在宇宙中的银河系中的太阳系里八大行星的地球上亚洲中国江苏"];
    //将tableview加入视图
    [self.view addSubview:self.tableView];
    
}
//懒加载
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}
//返回分组
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}
//每组元素个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 2;
}
//cell里显示的内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *identifierCell = [NSString stringWithFormat:@"Cell%ld%ld",(long)indexPath.section,(long)indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifierCell];
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
            reuseIdentifier:identifierCell];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;//点击cell没点击阴影效果
    if (indexPath.section == 0) {
        if (indexPath.row ==0){
            cell.userInteractionEnabled = NO;//设置cell不能点击
            //初始viewincell
            _viewincell = [[UIView alloc] init];
            _viewincell.frame = CGRectMake(0, 0, SCREEN_WIDTH, 80);
            label1 = [[UILabel alloc]init];
            label2 = [[UILabel alloc]init];
            label3 = [[UILabel alloc]init];
            label1.text = _ary1[0];
            label2.text = _ary1[1];
            label3.text = _ary1[2];
            [self addLabel];//调用添加label方法
            //将viewincell加入cell
            [cell addSubview:self.viewincell];
            
        }
        if (indexPath.row ==1) {
            //初始viewincell
            _viewincell = [[UIView alloc] init];
            _viewincell.frame = CGRectMake(0, 0, SCREEN_WIDTH, 40);
            [self addBtn];//添加btn方法
            //将viewincell加入cell
            [cell addSubview:self.viewincell];
        }
    }
    else if (indexPath.section == 1) {
        if (indexPath.row ==0){
            cell.userInteractionEnabled = NO;//设置cell不能点击
            //初始viewincell
            _viewincell = [[UIView alloc] init];
            _viewincell.frame = CGRectMake(0, 0, SCREEN_WIDTH, 80);
            label1 = [[UILabel alloc]init];
            label2 = [[UILabel alloc]init];
            label3 = [[UILabel alloc]init];
            label1.text = _ary2[0];
            label2.text = _ary2[1];
            label3.text = _ary2[2];
            [self addLabel];//调用添加label方法
            //将viewincell加入cell
            [cell addSubview:self.viewincell];
            
        }
        if (indexPath.row ==1) {
            //初始viewincell
            _viewincell = [[UIView alloc] init];
            _viewincell.frame = CGRectMake(0, 0, SCREEN_WIDTH, 40);
            [self addBtn];//添加btn方法
            //将viewincell加入cell
            [cell addSubview:self.viewincell];
        }
    }
    else if (indexPath.section == 2) {
        if (indexPath.row ==0){
            cell.userInteractionEnabled = NO;//设置cell不能点击
            _viewincell = [[UIView alloc] init];
            _viewincell.frame = CGRectMake(0, 0, SCREEN_WIDTH, 80);
            label1 = [[UILabel alloc]init];
            label2 = [[UILabel alloc]init];
            label3 = [[UILabel alloc]init];
            label1.text = _ary3[0];
            label2.text = _ary3[1];
            label3.text = _ary3[2];
            [self addLabel];
            [cell addSubview:self.viewincell];
        }
        if (indexPath.row ==1) {
            _viewincell = [[UIView alloc] init];
            _viewincell.frame = CGRectMake(0, 0, SCREEN_WIDTH, 40);
            [self addBtn];
            [cell addSubview:self.viewincell];
        }
    }
    return cell;
}
//设置cell高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        return 80;
    }
    else
        return 50;
}
//底部按钮
- (void)addAddressBtn{
    UIButton *addBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    //按钮位置
    addBtn.frame = CGRectMake(0, SCREEN_HEIGHT-BUTTOMBTN_H -64, SCREEN_WIDTH, BUTTOMBTN_H);
    addBtn.backgroundColor = [UIColor redColor];
    [addBtn setTitle:@"添加新地址" forState:UIControlStateNormal];
    [addBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    //添加按钮方法
    [addBtn addTarget:self action:@selector(pressaddAddress) forControlEvents:
     UIControlEventTouchUpInside];
    [self.view addSubview:addBtn];
}
- (void)pressaddAddress{
    NSLog(@"添加新地址");
}
//设置label
- (void)addLabel{
    //label位置
    label1.frame = CGRectMake(10, 5, 70, 20);
    label2.frame = CGRectMake(SCREEN_WIDTH-140, 5, 120, 20);
    label3.frame = CGRectMake(10, 30, SCREEN_WIDTH-20, 40);
    //字体大小
    label1.font = [UIFont systemFontOfSize:14];
    label2.font = [UIFont systemFontOfSize:14];
    label3.font = [UIFont systemFontOfSize:14];
    label2.textAlignment = NSTextAlignmentRight;//靠右对齐
    label3.numberOfLines = 2;//内容显示为两行
    [self.viewincell addSubview:label1];
    [self.viewincell addSubview:label2];
    [self.viewincell addSubview:label3];
}
//设置按钮
- (void)addBtn{
    btn1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn3 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn1.frame = CGRectMake(10, 10, 80, 20);
    btn3.frame = CGRectMake(SCREEN_WIDTH-60, 10, 40, 20);
    btn2.frame = CGRectMake(SCREEN_WIDTH -110, 10, 40, 20);
    [btn1 setTitle:@"设为默认" forState:UIControlStateNormal];
    [btn2 setTitle:@"编辑" forState:UIControlStateNormal];
    [btn3 setTitle:@"删除" forState:UIControlStateNormal];
    [btn1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [btn2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [btn3 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    //添加按钮方法
    [btn1 addTarget:self action:@selector(pressOk) forControlEvents:UIControlEventTouchUpInside];
    [btn2 addTarget:self action:@selector(pressEdit) forControlEvents:UIControlEventTouchUpInside];
    [btn3 addTarget:self action:@selector(pressDelete) forControlEvents:
     UIControlEventTouchUpInside];
    [self.viewincell addSubview:btn1];
    [self.viewincell addSubview:btn2];
    [self.viewincell addSubview:btn3];
}
- (void)pressOk{
    NSLog(@"设为默认");
}
- (void)pressEdit{
    NSLog(@"编辑");
}
- (void)pressCancel{
    NSLog(@"删除");
}
@end

运行结果:

 

220006_OYlq_2751796.png

转载于:https://my.oschina.net/Baidu1hao/blog/1592857

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值