0

我有一个包含项目列表的表格视图。当我单击其中一项时,我希望突出显示背景。我的代码可以正常工作,但颜色会在发布时发生变化,而不是在点击本身上。当用户点击单元格而不是当他/她释放它时,如何突出显示?

这是我的代码:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    // Navigation logic may go here. Create and push another view controller.

    Help_Cell *cell =(Help_Cell*) [tableView cellForRowAtIndexPath:indexPath];

    cell.selectionStyle=UITableViewCellSelectionStyleNone;

    UIView *v=[[UIView alloc] initWithFrame:cell.frame];

    v.backgroundColor=[self colorForHex:[appDel.appColorSettings objectForKey:@"cellColor2"]];

    cell.backgroundView=v;

    cell.title.textColor=[self colorForHex:[appDel.appColorSettings objectForKey:@"cellColor1"]];
}

我希望它像按钮一样发生。除此之外还有onClickUITableView 或 UITableViewCell 的方法didSelectRowAtIndexPath吗?

编辑

这是我的代码cellForRowAtIndexPath

-(void)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    Help_Cell *cell =(Help_Cell*) [tableView cellForRowAtIndexPath:indexPath];

    cell.selectionStyle=UITableViewCellSelectionStyleNone;

    UIView *v=[[UIView alloc] initWithFrame:cell.frame];

    v.backgroundColor=[self colorForHex:[appDel.appColorSettings objectForKey:@"cellColor2"]];

    cell.selectedBackgroundView=v;

}
4

2 回答 2

2

在创建单元格时,在 cellForRowAtIndexPath 中,编写以下代码:

UIView *v=[[UIView alloc] initWithFrame:cell.frame];
v.backgroundColor = [self colorForHex:[appDel.appColorSettings objectForKey:@"cellColor2"]];
    cell.selectedBackgroundView = v;

无需在 didSelectRow 方法中编写任何内容。

于 2012-08-09T12:22:33.463 回答
0

您可以将此视为选项

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

[tableView selectRowAtIndexPath:indexPath.row animated:YES scrollPosition:UITableViewScrollPositionTop];

}
于 2012-08-09T12:28:31.133 回答