添加到UITableViewCell的内容视图中的UIButton仅在选择单元格时出现

时间:2011-10-29 05:16:15

标签: iphone ios uitableview

我在尝试将按钮添加到UITableViewCell的内容视图时遇到问题。 无法看到按钮。但是,当我单击单元格并选择它时(使用默认的蓝背景),可以看到按钮。

使用以下代码创建表视图控制器:initWithStyle:UITableViewStyleGrouped

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UIButton *theButton;


UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

    theButton = [[UIButton alloc] initWithFrame:CGRectMake(800, 25, 32, 32)];
    [cell.contentView addSubview:theButton];
    theButton.tag = 1;
    [theButton release];

}

if(indexPath.section ==0){
        //...logic...
    }else if(indexPath.section ==1){
        //...logic...
    }else{
        cell.textLabel.text = @"some string";

   theButton = (UIButton*)[cell.contentView viewWithTag:1];
    [theButton setImage:theImage forState:UIControlStateNormal];
}

return cell;
}

1 个答案:

答案 0 :(得分:4)

我通过使用[UIColor clearColor]清除表格视图单元格的文本标签背景颜色来解决问题。