ios UITableView在表格重新出现时不会自动取消选中选中的行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12106032/
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 does not automatically deselect the selected row when the table re-appears
提问by Besi
Normally a selected row in a UITableView
gets deselected with an animation when the user pops back from the detail view.
通常UITableView
,当用户从详细信息视图中弹出时,a 中的选定行会通过动画取消选择。
However, in my case where I have a UITableView
embedded in a UIViewController
I have to do it manually in viewWillAppear
like so:
但是,在我UITableView
嵌入了 a 的情况下,我必须像这样UIViewController
手动执行viewWillAppear
:
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
// For some reason the tableview does not do it automatically
[self.tableView deselectRowAtIndexPath:self.tableView.indexPathForSelectedRow
animated:YES];
}
Why is this and how to fix it?
为什么会这样以及如何解决?
回答by Herm
When your main ViewController is from type UITableViewController, it has a propertyclearsSelectionOnViewWillAppear
, which is per default YES
- so it will clear the selection automatically.
当你的主 ViewController 来自UITableViewController类型时,它有一个属性clearsSelectionOnViewWillAppear
,这是默认的YES
- 所以它会自动清除选择。
This property is not available for an UITableView, i guess it's because it has no ViewWillAppear
method either.
此属性不适用于UITableView,我猜是因为它也没有ViewWillAppear
方法。
A UIViewControllerdoesn't need this property because it has no UITableView
originally.
一个UIViewController的,因为它没有也不需要这个属性UITableView
本来。
conclusion: you'll have to implement it by yourself when you do not use a UITableViewController
.
结论:当您不使用UITableViewController
.
回答by janusfidel
Do the deselection in didSelectRowAtIndexPath
instead of viewWillAppear
:
取消选择 indidSelectRowAtIndexPath
而不是viewWillAppear
:
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//show the second view..
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
回答by Kevin R
In swift, you can add the following lines in your viewWillAppear
在 swift 中,您可以在您的 viewWillAppear
if let row = tableView.indexPathForSelectedRow() {
tableView.deselectRowAtIndexPath(row, animated: true)
}
In swift 2, it's without parantheses:
在 swift 2 中,它没有括号:
if let row = tableView.indexPathForSelectedRow {
tableView.deselectRowAtIndexPath(row, animated: true)
}
In Swift 4 (and 3?) the function name was cleaned up:
在 Swift 4(和 3?)中,函数名称被清理:
if let indexPath = tableView.indexPathForSelectedRow {
tableView.deselectRow(at: indexPath, animated: true)
}
回答by Neo
I dont think deselecting the selected row is automatic... I normally do it before pushing to the next view
我不认为取消选择所选行是自动的......我通常在推送到下一个视图之前这样做
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
// to do other things
[self.navigationController pushViewController:yourNextViewController animated:YES];
}
回答by nielsbot
Nothing is wrong--deselecting the highlighted row is always "manual". If you look at Apple's sample code, you will see the same thing.
没有错——取消选择突出显示的行总是“手动”。如果您查看 Apple 的示例代码,您会看到相同的内容。
回答by oscar castellon
In Swift 3 / 4
在斯威夫特 3 / 4
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
}
回答by Scott Ahten
If table view selections are not being cleared in a subclass of UITableViewController
, despite the fact that clearsSelectionOnViewWillAppear
is set to true, make sure to call the superclass version of [UIViewController viewWillAppear:animated]
if you override that method.
如果在 的子类中未清除表视图选择UITableViewController
,尽管clearsSelectionOnViewWillAppear
设置为 true,请确保在[UIViewController viewWillAppear:animated]
覆盖该方法时调用 的超类版本。
If you fail to call the super version of that method, the value of clearsSelectionOnViewWillAppear
will have no effect as the work of clearing the table view selection is actually performed in UITableViewController
's implementation of viewWillAppear
.
如果您没有调用该方法的超级版本,则 的值clearsSelectionOnViewWillAppear
将不起作用,因为清除表视图选择的工作实际上是在UITableViewController
的实现中执行的viewWillAppear
。
If your view controller does not inherit from UITableViewController
you will need to clear the selection manually in viewWillAppear
.
如果您的视图控制器不继承自UITableViewController
您将需要在viewWillAppear
.