如何在选择TableView单元格时防止按钮消失?

时间:2012-05-09 13:49:51

标签: ios ipad xcode4

我在TableView中有一个自定义单元格,单元格中有一个按钮。当我选择单元格时,背景变为蓝色,按钮消失。有什么方法可以阻止这种情况吗?

像这样的cellForRowAtIndexPath -

- (UITableViewCell *)tableView:(UITableView *)tableView
     cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
MyTableCell *cell = (MyTableCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
    cell = [[MyTableCell alloc] initWithStyle:UITableViewCellStyleSubtitle
                                  reuseIdentifier:CellIdentifier];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

}
Search *current = [self.retrievedTweets objectAtIndex:indexPath.row];
cell.textLabel.text = current.text;
cell.textLabel.font = [UIFont systemFontOfSize:15.0];
cell.textLabel.numberOfLines = 2;
cell.detailTextLabel.text = current.name;
cell.imageView.image = current.userImage;


btnUncheck =[[UIButton alloc] initWithFrame:CGRectMake(400, 35, 20, 20)];


btnUncheck.backgroundColor = [UIColor redColor];
btnUncheck.layer.cornerRadius = 10;
btnUncheck.hidden =NO;
btnUncheck.tag=indexPath.row;
//[btnUncheck addTarget:self action:@selector(didSelectRowAtIndexPath:) forControlEvents:UIControlEventTouchUpInside];

[cell.contentView addSubview:btnUncheck];
return cell;
}

6 个答案:

答案 0 :(得分:1)

为选定的表格单元格使用自定义图像。或尝试使用按钮图像。

答案 1 :(得分:0)

在tableview委托方法

中粘贴到下面的行
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

///// your code ///////////
cell.selectionStyle=UITableViewCellSelectionStyleNone;
//// your code////////

}

这里你的蓝色背景不会显示..

答案 2 :(得分:0)

你可以做的是:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{[tableView deselectRowAtIndexPath:indexPath animated:YES];}

希望这有帮助

另外,如果你已经实现了这个,那么你在这个委托方法中还有什么呢?

答案 3 :(得分:0)

而不是这一行:

btnUncheck = [[UIButton alloc] initWithFrame:CGRectMake(400, 35, 20, 20)];

使用:

btnUncheck = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
[btnUncheck setFrame:CGRectMake(400, 35, 20, 20)];

答案 4 :(得分:0)

创建一个“自定义”视图,背景颜色清晰并设置为它。

UIView* vista=[[[UIView alloc] init] autorelease];
vista.backgroundColor=[UIColor clearColor];
cell.selectedBackgroundView=vista;

答案 5 :(得分:0)

解决这个问题的方法是创建一个自定义单元格,然后在xib编辑器中添加一个按钮。

因此,在xib编辑器中,添加一个'UITableViewCell'项。然后在xib编辑器中将该按钮添加到该单元格项。然后,您将自定义单元格附加到相应的.m文件中的表格中:

  • (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

我已经尝试了这个,只有一行表,自定义单元格作为属性从xib绑定到具有表视图的.h文件。突出显示在那里,只是按下按钮(仍可查看)。要让更多行使用此功能更为复杂。您需要加载自定义的utableviewcell xib并动态分配自定义单元格以使其正常工作。