Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在UITableView单元格中以编程方式创建n个UIButton?_Ios_Objective C_Uitableview_Uibutton - Fatal编程技术网

Ios 如何在UITableView单元格中以编程方式创建n个UIButton?

Ios 如何在UITableView单元格中以编程方式创建n个UIButton?,ios,objective-c,uitableview,uibutton,Ios,Objective C,Uitableview,Uibutton,我正在尝试创建一个UITableView,其中n个按钮的数量取决于后端JSON数据 我附加了一个图像,我知道如何在UITableViewCell上创建UIButtons,但我不知道如何将它们正确放置在UITableViewCell中 UITableViewCell的ui按钮 UIButton *continuebtn = [[UIButton alloc]initWithFrame:CGRectMake(10, 100, view1.frame.size.width-20, 40)]; [con

我正在尝试创建一个
UITableView
,其中n个按钮的数量取决于后端JSON数据

我附加了一个图像,我知道如何在
UITableViewCell
上创建UIButtons,但我不知道如何将它们正确放置在
UITableViewCell

UITableViewCell的
ui按钮

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
上放置
UIButton
的“n”号
ui按钮
宽度取决于其文本内容

如果要在单元格中垂直放置按钮,请使用以下建议:

UITableviewCell
的高度取决于按钮的数量。按如下所示实施“高度为行”索引路径的
heightforrowatinexpath
方法:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 100.0f + (buttonsArray.count*buttonHeight+buttonsHeightSeparator);

    //change 100.0f with height that is required for cell without buttons
    //buttonHeight is a static float representing value for height of each button
    //buttonHeightSeparator is a static float representing separation distance between two buttons
}
cellforrowatinexpath
方法中,可以使用以下代码创建按钮:

for(int i=0; i<buttonsArray.count; i++) {
    UIButton *continuebtn = [[UIButton alloc]initWithFrame:CGRectMake(10, 100+i*(buttonHeight+buttonsHeightSeparator), 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];
    [continuebtn addTarget:self action:@selector(continueBtnPressed:) forControlEvents:UIControlEventTouchUpInside]; //add target to receive button tap event
    [continuebtn setTag:i]; //to identify button
    [cell.contentView addSubview:continuebtn]; //add button to cell
}

for(inti=0;i我自己找到了一个更好的答案。
请检查这个代码

  - (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;
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
cell=[tableView dequeueReusableCellWithIdentifier:@“cell”forIndexPath:indexPath];
[cell addSubview:[self addView:indexPath.row]];
返回单元;
}
-(UIView*)添加视图:(NSInteger)行{
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=(uiviewAutoresizingflexibleleleftmargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin);
int x,y;
x=10;y=10;
for(inti=0;iprogView.frame.size.width){
y+=50;
x=10;
}
button.layer.borderWidth=1.0;
button.layer.borderColor=[UIColor greenColor].CGColor;
[按钮设置标题颜色:[UIColor blackColor]用于状态:UIControlStateNormal];
[按钮设置标签:i];
[程序视图添加子视图:按钮];
}
返回程序视图;
}
-(CGFloat)tableView:(UITableView*)表视图行高度索引路径:(NSIndexPath*)索引路径
{
返回200;
}

单元格可以水平滚动?不,不是这样,但单元格高度应该增加,以自动适应所有按钮。这就是问题所在。因此,您会得到数据,即您要添加多少按钮,如3、4、5 etcc?每个按钮将被放置为水平板,或者它们将像集合视图中那样排列?是的,正确,然后我将用于循环生成UIButton如果我的UITableView单元格中UIButton的数量不同,那么应该创建多个部分,还是应该检查UITableView中的indexpath.row?检查
heightForRowAtIndexPath
cellForRowAtIndexPath
方法中的indexpath.row,以便在不同的单元格中创建按钮。此creating按钮垂直独立于TextSize将单元格高度设置为200的恒定值,如果有更多超过200的按钮,这不会截断单元格吗?