UITableView选择并取消选择行

时间:2013-12-15 20:47:46

标签: ios uitableview

我有一个UITableViewcell,触摸它后会保持高亮显示。我想知道如何在触摸后立即删除高亮显示。

因此,当您触摸UITableViewCell时,我希望它被选中并突出显示,然后当用户举起手指时,我想取消选择并取消选中UITableViewCell。

这是我到目前为止所做的,取消选择有效,但仍然突出显示单元格。

#pragma mark -- select row
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    NSLog(@"%@", indexPath);
    NSLog(@"%@", cell.textLabel.text);

}

#pragma mark -- deselect row
-(void) tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {

    [tableView deselectRowAtIndexPath:indexPath animated:YES];

}

任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:33)

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

这是一个无限循环,我很确定。但是......它有点走上正轨。您可以将该方法调用移至didSelectRowAtIndexPath:

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:
    (NSIndexPath *)indexPath {
    //stuff
    //as last line:
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

就此而言,可以随时随地调用deselectRowAtIndexPath来取消选择行。

[self.myTableView deselectRowAtIndexPath:[self.myTableView 
    indexPathForSelectedRow] animated: YES];

答案 1 :(得分:0)

您需要取消选择didSelectRowAtIndexPath方法

上的行

示例代码

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