如何在UITableView Cell中以编程方式创buildn个UIButton?
我想创build一个具有n个button的UITableView
取决于后端JSON数据。
我已经附加了一个图像,我知道如何在UITableViewCell
上创buildUIButtons,但我不知道如何将它们正确放置在UITableViewCell
。
用于UITableViewCell
UIButton
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
宽度取决于其文本内容
如果要将button垂直放置在单元格中,请使用以下build议:
你的UITableviewCell
的高度将取决于button的数量。 实现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
方法中,您可以使用以下代码创buildbutton:
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 }
我自己find了更好的答案。 请检查这个代码。
- (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; }