ios - 向下滚动页面一次后显示表格 View 按钮

标签 ios uitableview

我正在尝试向 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/

相关文章:

ios - 如何从设备制作唯一标识符?

ios - 为什么 MPMoviePlayerController 在寻找 HTTP Live 流后报告错误的时间戳

ios - 如何仅获取设备上每个联系人的第一个电话号码?

ios - 在自定义 UITableViewCell 中,在哪里将 Observer 添加到 NSNotificationcenter?

ios - 将变量从 tableView 传递到 prepareForSegue?

ios - Xcode 4.6 iOS 设置 ViewController 的类

ios - 使用 Instapaper API 时,为什么我不断收到 401 错误?

ios - 如何在 UITableView 的 Realm 中按日期对对象进行分组?

iphone - 下拉 UItableView 切换 View Controller

ios - 禁用特定 UITableView 部分的分隔符