从 segue 返回时,UITableViewCells 不会取消选择

发布于 2024-12-21 07:25:11 字数 219 浏览 3 评论 0原文

我有一个 UITableView,其中包含 Storyboard 中的单元格,以及将单元格连接到另一个视图的 segue。

当您选择一个单元格时,它会显示单元格选择动画(在我的例子中将单元格变成灰色)并将另一个视图推到屏幕上。但是当您返回到表格视图时,取消选择动画根本不显示(与选择动画相反)。由于我只是使用转场,所以我希望默认情况下会处理这个问题。

有什么办法可以强制它显示取消选择动画吗?

I have a UITableView with cells in a Storyboard, and a segue connecting the cells to another view.

When you select a cell it displays the cell selection animation (turning the cell gray, in my case) and pushes the other view onto the screen. But when you return to the tableview, the deselection animation doesn't show at all (the reverse of the selection animation). Since I'm just using a segue, I expected this to be taken care of by default.

Is there any way to force it to show the deselection animation?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

静谧幽蓝 2024-12-28 07:25:11

如果您的视图控制器是 UITableViewController 的子类,并且 clearsSelectedOnViewWillAppear 设置为 YES(这是默认值),这将自动处理。

在您的情况下,您可以按照与 UITableViewController 相同的方式执行此操作。取消选择 -viewWillAppear: 中选定的行。

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    NSIndexPath *selectedIndexPath = [self.tableView indexPathForSelectedRow];
    [self.tableView deselectRowAtIndexPath:selectedIndexPath animated:YES];
}

This would be handled automatically if your view controller was a subclass of UITableViewController and clearsSelectedOnViewWillAppear was set to YES (which is the default value).

In your case you can do this the same exact way that UITableViewController does it. Deselect the selected row in -viewWillAppear:.

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    NSIndexPath *selectedIndexPath = [self.tableView indexPathForSelectedRow];
    [self.tableView deselectRowAtIndexPath:selectedIndexPath animated:YES];
}
煮酒 2024-12-28 07:25:11

不确定 Segues 的使用,但我经常想在视图控制器出现时刷新数据。但是,如果重新加载表,则会清除所选行。这是我用来维护选定行并在返回时显示取消选择动画的一些代码。也许这对您有帮助,所以我将其发布在这里。

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    NSIndexPath *indexPath = [tableView indexPathForSelectedRow];
    [tableView reloadData];
    if(indexPath) {
        [tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
    }
}

-(void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];

    NSIndexPath *indexPath = [tableView indexPathForSelectedRow];
    if(indexPath) {
        [tableView deselectRowAtIndexPath:indexPath animated:YES];  
    }
}

Not sure about the use of segues but I often want to refresh data when a view controller appears. If you reload the table however, you clear the select row. Here is some code I use to maintain a selected row and show the deselect animation when returning. It may be the case that this may help you so I will post it here.

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    NSIndexPath *indexPath = [tableView indexPathForSelectedRow];
    [tableView reloadData];
    if(indexPath) {
        [tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
    }
}

-(void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];

    NSIndexPath *indexPath = [tableView indexPathForSelectedRow];
    if(indexPath) {
        [tableView deselectRowAtIndexPath:indexPath animated:YES];  
    }
}
清泪尽 2024-12-28 07:25:11

确保您正在调用 viewWill...viewDid... 方法的 super 实现

Make sure you are calling the super implementations of the viewWill... and viewDid... methods

千と千尋 2024-12-28 07:25:11

对于斯威夫特 3

override func viewWillAppear(_ animated: Bool) {

    super.viewWillAppear(animated)
    if let path = tableView.indexPathForSelectedRow {

        tableView.deselectRow(at: path, animated: true)
    }
}

For Swift 3

override func viewWillAppear(_ animated: Bool) {

    super.viewWillAppear(animated)
    if let path = tableView.indexPathForSelectedRow {

        tableView.deselectRow(at: path, animated: true)
    }
}
凤舞天涯 2024-12-28 07:25:11

快速更新:-

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(true)
    if tableView.indexPathForSelectedRow != nil {
        let indexPath: NSIndexPath = tableView.indexPathForSelectedRow!
        tableView.deselectRowAtIndexPath(indexPath, animated: true)
    }
}

Swift Update :-

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(true)
    if tableView.indexPathForSelectedRow != nil {
        let indexPath: NSIndexPath = tableView.indexPathForSelectedRow!
        tableView.deselectRowAtIndexPath(indexPath, animated: true)
    }
}
财迷小姐 2024-12-28 07:25:11

更好的 Swift 更新:

 override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)

    if let path = tableView.indexPathForSelectedRow {

        tableView.deselectRowAtIndexPath(path, animated: true)
    }
}

Better Swift update:

 override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)

    if let path = tableView.indexPathForSelectedRow {

        tableView.deselectRowAtIndexPath(path, animated: true)
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文