Ios TableView取消RowAtIndexPath不调用setHighlighted

Ios TableView取消RowAtIndexPath不调用setHighlighted,ios,uitableview,Ios,Uitableview,我正在尝试更改表格视图单元格中标签的状态。 我希望在按下其他视图控制器并使用tableview移回视图控制器时保持选定单元格。 当我选择另一行时,我希望删除先前选定行标签的突出显示(取消选择先前选定的行)并突出显示当前行的标签。 -(void)取消行索引路径:(nsindepath*)indepath动画:(BOOL)动画应该调用-(void)setHighlighted:(BOOL)highlighted动画:(BOOL)动画,并为该单元格突出显示“否”? 注意:我不是UITableViewC

我正在尝试更改表格视图单元格中标签的状态。
我希望在按下其他视图控制器并使用tableview移回视图控制器时保持选定单元格。
当我选择另一行时,我希望删除先前选定行标签的突出显示(取消选择先前选定的行)并突出显示当前行的标签。

-(void)取消行索引路径:(nsindepath*)indepath动画:(BOOL)动画
应该调用
-(void)setHighlighted:(BOOL)highlighted动画:(BOOL)动画
,并为该单元格突出显示“否”?

注意:我不是UITableViewController。

保持单元格处于选中状态:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.selected = YES;
    //Other code 
}
确保类单元格未在interface builder中选择=无。

取消选择最后一行:

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSIndexPath *currentSelectedIndexPath = [tableView indexPathForSelectedRow];
    if (currentSelectedIndexPath)
    {
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        cell.selected = NO;
    }
    return indexPath;
}

首先,应该正确设置TableviewCell[cell setSelectionStyle:UITableViewCellSelectionStyleGray];在方法上:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
除此之外,您还可以通过以下方式设置所选单元格的自定义背景:

UIView *bgColorView = [[UIView alloc] init];
bgColorView.backgroundColor = [UIColor colorWithWhite:0.97 alpha:1.0];
bgColorView.layer.masksToBounds = YES;
[cell setSelectedBackgroundView:bgColorView];
对于突出显示选定单元格的问题,可以使用简单的标志,如

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSIndexPath *ip= [NSIndexPath indexPathForRow:flagForLastSelectedRow inSection:0];
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    flagForLastSelectedRow=indexPath.row;
}

使用UITableViewController时,默认设置是在弹出后显示视图时清除选择。但是你说你没有使用它,我想这意味着它是你自己控制器中的一个UITableView。您是否发现该单元格未保持选中状态?我本以为在选择时,它会保持选中状态,除非您自己清除它或重新加载数据。如果已将表格设置为单选,则选择其他行应自动取消选择并选择新行。所以这应该在默认情况下发生,除非您正在重新加载一个代理中的单元格?