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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 19:51:37  来源:igfitidea点击:

UITableView does not automatically deselect the selected row when the table re-appears

iphoneobjective-cioscocoa-touchuitableview

提问by Besi

Normally a selected row in a UITableViewgets deselected with an animation when the user pops back from the detail view.

通常UITableView,当用户从详细信息视图中弹出时,a 中的选定行会通过动画取消选择。

However, in my case where I have a UITableViewembedded in a UIViewControllerI have to do it manually in viewWillAppearlike 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 ViewWillAppearmethod either.

此属性不适用于UITableView,我猜是因为它也没有ViewWillAppear方法。

A UIViewControllerdoesn't need this property because it has no UITableVieworiginally.

一个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 didSelectRowAtIndexPathinstead 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 clearsSelectionOnViewWillAppearis 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 clearsSelectionOnViewWillAppearwill 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 UITableViewControlleryou will need to clear the selection manually in viewWillAppear.

如果您的视图控制器不继承自UITableViewController您将需要在viewWillAppear.