【问题标题】:Reloading Table and Clearing Cell Row Selection重新加载表格并清除单元格行选择
【发布时间】:2014-11-03 18:53:59
【问题描述】:

直到 iOS 8 和 Xcode 6,我都能够使用 UITableViewController 来配置我的应用程序,就像 Apple 的设置一样。在从一个表返回调用表时,我会重新加载调用表以更新单元格中的数据,重新选择调用行,然后让视图显示为动画以清除选择。

这是我一直在使用的 viewWillAppear 方法中的代码:

[self.tableView reloadData];
[self.tableView selectRowAtIndexPath:lastselected animated:YES scrollPosition:UITableViewScrollPositionNone];
[super viewWillAppear:animated];

当单元格数据更新时,所选行/单元格的缓慢消失不会发生。它立即被有效清除。我已经尝试了几乎所有我能想到的并且无法使其正常工作。请帮忙...

发生了什么变化,我怎样才能恢复这种良好的过渡?

谢谢。

【问题讨论】:

    标签: uitableview ios8 xcode6


    【解决方案1】:

    viewWillAppear 在您的视图出现在屏幕上之前被调用。因此,如果从该方法内部触发,您将无法使任何动画正常工作。改用viewDidAppear,您应该会看到更可靠的过渡动画。如果您觉得它发生得太快,也可以延迟使用performSelector。

    就个人而言,在这种情况下,我会先致电[super viewDidAppear:animated];。我看不出它低于其他代码行的原因。

    还只是为了让我 100% 清楚...viewWillAppear 显然可以重新加载 tableView,所以让我们这样做...

    -(void)viewWillAppear:(BOOL)animated
    {
        [super viewWillAppear:animated];
        [self.tableView reloadData];
    }
    
    
    -(void)viewDidAppear:(BOOL)animated
    {
        [super viewDidAppear:animated];
        [self.tableView selectRowAtIndexPath:lastSelected animated:YES scrollPosition:UITableViewScrollPositionNone];
    }
    

    或者在 viewDidAppear 上执行选择器延迟...

    -(void)viewDidAppear:(BOOL)animated
    {
        [super viewDidAppear:animated];
        [self performSelector:@selector(animateCellSelectionAtIndexPath:) withObject:lastSelected afterDelay:0.2f];
    }
    
    -(void)animateCellSelectionAtIndexPath:(NSIndexPath *)indexPath
    {
         [self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
    }
    

    【讨论】:

    • 谢谢。这实际上并没有像你计划的那样有帮助,但我可以通过这样做来让它工作: -(void)viewWillAppear: (BOOL)animated { [self.tableView reloadData]; [self.tableView selectRowAtIndexPath:lastselected animated:YES scrollPosition:UITableViewScrollPositionNone]; [self performSelector:@selector(animateCellSelectionAtIndexPath:) withObject:lastselected afterDelay:0.05f]; } -(void)animateCellSelectionAtIndexPath:(NSIndexPath *)indexPath { [self.tableView deselectRowAtIndexPath:indexPath animated:YES]; }
    • 在您的评论代码中,您执行了两次selectRowAtIndexPath 调用。您不应该只需要第一个selectRowAtIndexPath,而只需要延迟执行选择器中的那个。如果我的 viewDidAppear 版本不起作用,我会感到非常惊讶,尽管我很高兴这种情况下的延迟为你解决了......正如我所说,你不能保证这一点,因为从技术上讲它是一个不确定的数量在它“确实出现”之前的时间。
    • 很好。它实际上是一个 deselectRowAtIndexPath 而不是另一个选择。复制/粘贴错误。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-23
    • 2019-03-11
    • 1970-01-01
    • 2012-06-01
    • 2018-02-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多