作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试创建一个带有 n 个按钮的 UITableView
,具体取决于后端 JSON 数据。
我附上了一张图片,我知道如何在 UITableViewCell
上创建 UIButton,但我不知道如何将它们正确放置在 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];
如何在 UITableViewCell
上放置 'n' 个 UIButton
?? UIButton
宽度取决于其文本内容
最佳答案
如果要将按钮垂直放置在单元格中,请使用以下建议:
您的 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
}
关于ios - 如何在 UITableView Cell 中以编程方式创建 n 个 UIButton?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33496982/
我是一名优秀的程序员,十分优秀!