我正在尝试使用如下代码向我的 UITableView 单元格添加按钮:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
UIButton *button = self.buttons[0];
[cell addSubview:button];
return cell;
}
虽然按钮没有显示。是否可以像这样向单元格添加按钮,或者我是否必须制作自定义 UITableViewCell 类?
当为数组制作按钮时,框架设置如下:
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 70, 70)]
最佳答案
添加元素的用法在某些时候发生了变化。使用
[cell.contentView addSubview:button];
并且如果已经添加了按钮,请不要忘记在之前进行验证 :P
enum
{
kButtonForA = 3333 // example
};
对于每个按钮集
button.tag = kButtonForA;
并使用它们
UIButton* btn = (UIButton *)[cell.contentView viewWithTag:kButtonForA];
if(!button)
btn = self.buttons[i];
更新
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
if(!self.buttons || [self.buttons count] == 0)
return 0;
return ...;
}
在你初始化你的数组之后,使用
[tableView reloadData];
只是一种可能的方式,但是一种快速的方式
关于ios - 将按钮添加到 UITableView 单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28428185/