ios UITableView 选择和取消选择行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20599597/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UITableView select and deselect row
提问by HurkNburkS
I have a UITableViewcell that stays highlighted after touching it. I would like to know how to remove the highlight right after it becomes visible after your touch.
我有一个 UITableViewcell 在触摸它后保持突出显示。我想知道如何在触摸后立即删除突出显示。
So when you touch the UITableViewCell I would like it to become selected and highlighted then when the user raises their finger I would like to deselect and unhighlight the UITableViewCell.
因此,当您触摸 UITableViewCell 时,我希望它被选中并突出显示,然后当用户抬起手指时,我希望取消选择并取消突出显示 UITableViewCell。
This is what I am doing so far, and the deselect works but the cell is still highlighted.
这就是我到目前为止所做的,取消选择有效,但单元格仍然突出显示。
#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];
}
回答by nhgrif
-(void) tableView:(UITableView *)tableView didDeselectRowAtIndexPath:
(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
That's an infinite loop, I'm quite certain. However... it's sort of on the right track. You can move that method call into didSelectRowAtIndexPath:
.
这是一个无限循环,我很确定。然而......它有点走在正确的轨道上。您可以将该方法调用移动到didSelectRowAtIndexPath:
.
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:
(NSIndexPath *)indexPath {
//stuff
//as last line:
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
For that matter, deselectRowAtIndexPath
can be called from anywhere at any time you want the row to be deselected.
就此而言,deselectRowAtIndexPath
可以随时从任何您希望取消选择该行的地方调用。
[self.myTableView deselectRowAtIndexPath:[self.myTableView
indexPathForSelectedRow] animated: YES];
回答by Vivek
You need to deselect row on didSelectRowAtIndexPath
method
您需要在didSelectRowAtIndexPath
方法上取消选择行
Example code
示例代码
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:NO];
}