如何在UITableView Cell中以编程方式创建n个UIButton?

时间:2015-11-03 10:38:29

标签: ios objective-c uitableview uibutton

我正在尝试使用n个按钮创建一个UITableView,这取决于后端JSON数据。

我附上了一张图片,我知道如何在UITableViewCell上创建UIButtons,但我不知道如何在UITableViewCell内正确放置它们。

UIButton

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];

enter image description here

如何在UIButton上放置{n'个UITableViewCellUIButton宽度取决于其文字内容

2 个答案:

答案 0 :(得分:1)

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

UITableviewCell的高度取决于按钮的数量。实施heightForRowAtIndexPath方法如下:

- (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
}

cellForRowAtIndexPath方法中,您可以使用以下代码创建按钮:

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
}

答案 1 :(得分:1)

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

  - (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;
}