【问题标题】:selectRowAtIndexPath on a UITableViewControllerUITableViewController 上的 selectRowAtIndexPath
【发布时间】:2021-01-11 08:27:39
【问题描述】:

我试图在 UITableView 中显示视图出现时选择的行。我可以在 UIViewController 上执行此操作,但是当我尝试在 UITableViewController 上执行此操作时,它不起作用。

我在 ViewDidAppear 中调用它:

NSIndexPath *selectedIndex = [self.tableView indexPathForSelectedRow];
[self.tableView selectRowAtIndexPath:selectedIndex animated:YES scrollPosition:UITableViewScrollPositionNone];

有没有其他方法可以在 UITableViewController 中实现这一点?

【问题讨论】:

    标签: ios objective-c xcode uitableview


    【解决方案1】:

    看起来selectedIndex 为 nil,表示没有选择当前行。

    您需要跟踪要选择的行,然后使用selectRowAtIndexPath: 进行设置

    要验证这是否有效,请使用现有行尝试:

    NSIndexPath *selectedIndex = [NSIndexPath indexForRow: 1 inSection: 0];
    [self.tableView selectRowAtIndexPath:selectedIndex animated:YES scrollPosition:UITableViewScrollPositionNone];
    

    【讨论】:

      【解决方案2】:

      我发现表格没有滚动到所选行的原因有两个。

      因为我以编程方式选择行,并且使用的是 scrollPosition:UITableViewScrollPositionNone。使用这个,表格没有移动到所需的行。我将其更改为 scrollPosition:UITableViewScrollPositionTop。

      另一个问题是我在 ViewDidAppear 中重新加载表视图。将其更改为 ViewDidLoad 会在保持所选行外观方面有所不同。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-12-05
        • 1970-01-01
        • 2011-06-10
        • 2015-01-22
        • 1970-01-01
        相关资源
        最近更新 更多