我正在尝试为表格视图中的每个单元格添加一个自定义单选按钮。当我第一次查看表视图时,我看不到任何单选按钮。但是当我向下滚动时,我可以在第一次加载视图时看到的初始单元格下面的每个单元格上看到单选按钮。一旦没有单选按钮的单元格消失,当我返回查看该单元格时,单选按钮就会出现。
下面是我为这个方法编写的代码:
- (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;
}
如果你不明白这个问题,请告诉我。
发布于 2012-08-24 19:59:13
您正在用每个新的单元格覆盖_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)
块中,以便为每个单元格只创建一次。
https://stackoverflow.com/questions/12114308
复制相似问题