单击时突出显示表格视图单元格,而不是发布

问题描述:

我有一个包含项目列表的表格视图。当我点击其中一个项目时,我想让背景突出显示。我有代码工作,但颜色在发布时发生变化,而不是点击本身。如何突出显示用户点击单元格时而不是他/她发布单元格时的情况?单击时突出显示表格视图单元格,而不是发布

这里是我的代码:

-(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"]]; 
} 

我希望它发生像一个按钮。除了didSelectRowAtIndexPath之外,还有一个用于UITableView或UITableViewCell的onClick方法吗?

编辑

这里是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; 

} 

我的代码创造细胞,在的cellForRowAtIndexPath的时间,写下面的代码:

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

无需写任何东西在didSelectRow方法中。

+0

我喜欢这个想法,但补充说,在我的'cellForRowAtIndexPath'不工作。如果我将另一个'UIView v'设置为背景视图,会受到影响吗? – BigT 2012-08-09 12:43:53

+0

你应该为selectedBackgroundView添加它。不适用于backgroundView。如果你有其他视图作为backgroundView,它不会产生任何问题。也发布一些代码,以便我可以检查。 – Apurv 2012-08-09 12:46:23

+0

检查我的编辑。那就是我根据你的建议投入的。 – BigT 2012-08-09 12:51:15

你可以认为这是选项

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

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

}