我正在尝试向 TableView 中的每个单元格添加自定义单选按钮。当我第一次查看表格 View 时,我看不到任何单选按钮。但是,当我向下滚动时,我可以在首次加载 View 时看到的初始单元格下方的每个单元格上看到单选按钮。一旦没有单选按钮的单元格消失,当我返回查看该单元格时,单选按钮就会出现。
这是我针对此方法的代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *ImageCellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ImageCellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ImageCellIdentifier];
cell.accessoryType = UITableViewCellAccessoryNone;
}
_radioBtn.frame = CGRectMake(275, 3,36,36);
_radioBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[_radioBtn setImage:[UIImage imageNamed:@"Radio-Btn.png"] forState:UIControlStateNormal];
[cell.contentView addSubview:_radioBtn];
NSString *cellValue = [_arrayRelat objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;
cell.textLabel.textColor = [UIColor colorWithRed:(88.0/255.0) green:(88.0/255.0) blue:(89.0/255.0) alpha:1];
return cell;
}
如果您不明白这个问题,请告诉我。
最佳答案
您正在用每个新单元格覆盖 _radioBtn! 您应该为每个单元格创建一个新的 UIButton 实例,例如:
UIButton *radioBtn = [UIButton buttonWithType:UIButtonTypeCustom];
radioBtn.frame = CGRectMake(275, 3,36,36);
[radioBtn setImage:[UIImage imageNamed:@"Radio-Btn.png"] forState:UIControlStateNormal];
[cell.contentView addSubview:radioBtn];
并且您应该将其放入 if (cell == nil)
block 中,以便仅为每个单元格创建一次。
关于ios - 向下滚动页面一次后显示表格 View 按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12114308/