如何在 UITableView Cell 中以编程方式创建 n 个 UIButton? [英] How to create n number of UIButton programmatically in UITableView Cell?

查看:20
本文介绍了如何在 UITableView Cell 中以编程方式创建 n 个 UIButton?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个带有 n 个按钮的 UITableView 取决于后端 JSON 数据.

I am trying to create one UITableView with n number of buttons depends on backend JSON data.

我附上了一张图片,我知道如何在 UITableViewCell 上创建 UIButton,但我不知道如何将它们正确放置在 UITableViewCell 中.

I have attached an image, i know how to create UIButtons on UITableViewCell but i don't know how to place them correctly inside UITableViewCell.

UIButton for UITableViewCell

UIButton *continuebtn = [[UIButton alloc]initWithFrame:CGRectMake(10, 100, view1.frame.size.width-20, 40)];
[continuebtn setBackgroundColor:[UIColor grayColor]];
[continuebtn setTitle:@"Continue" forState:UIControlStateNormal];
continuebtn.layer.cornerRadius = 10;
continuebtn.layer.borderWidth =1.0;
continuebtn.layer.borderColor = [UIColor blackColor].CGColor;
[continuebtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

如何在UITableViewCell上放置'n'个UIButton ??UIButton 宽度取决于它的文本内容

How to place 'n' number of UIButton on UITableViewCell ?? UIButton width depends on its text content

推荐答案

我自己找到了更好的答案.请检查此代码.

I have found a better answer myself. Please check this code.

  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];

    [cell addSubview:[self addView:indexPath.row]];
    return cell;
}


-(UIView *)addView:(NSInteger)row{

    UIView *progView;
    progView = [[UIView alloc] initWithFrame:CGRectMake(0, 0,cell.frame.size.width,cell.frame.size.height)];
    progView.backgroundColor = [UIColor grayColor];
    progView.tag = i;    
    progView.autoresizingMask = (UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin);

    int x,y;
    x=10;y=10;
    for(int i=0;i<10;i++){
        UIButton *button = [[UIButton alloc]init];
        NSString *myString=@"Dynamic";
        [button setTitle:myString forState:UIControlStateNormal];
        CGSize stringsize = [myString sizeWithFont:[UIFont systemFontOfSize:14]];
        [button setFrame:CGRectMake(x,y,stringsize.width+40, stringsize.height+20)];
        x+=stringsize.width+50;
        NSLog(@"%d-%f",x,progView.frame.size.width);

        if(x>progView.frame.size.width){
            y+=50;
            x=10;
        }

        button.layer.borderWidth =1.0;
        button.layer.borderColor = [UIColor greenColor].CGColor;
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [button setTag:i]; 
        [progView addSubview:button];
    }

    return progView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 200;
}

这篇关于如何在 UITableView Cell 中以编程方式创建 n 个 UIButton?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆