[英]selectRowAtIndexPath on a UITableViewController
我试图在视图出现时在 UITableView 中显示选定的一行。 我可以在 UIViewController 上执行此操作,但是当我尝试在 UITableViewController 上执行此操作时,它不起作用。
我在 ViewDidAppear 中调用它:
NSIndexPath *selectedIndex = [self.tableView indexPathForSelectedRow];
[self.tableView selectRowAtIndexPath:selectedIndex animated:YES scrollPosition:UITableViewScrollPositionNone];
是否有不同的方法可以在 UITableViewController 中实现这一点?
看起来selectedIndex
为零,这意味着没有选择当前行。
您需要跟踪要选择的行,然后使用selectRowAtIndexPath:
设置它selectRowAtIndexPath:
要验证这是否有效,请使用现有行进行尝试:
NSIndexPath *selectedIndex = [NSIndexPath indexForRow: 1 inSection: 0];
[self.tableView selectRowAtIndexPath:selectedIndex animated:YES scrollPosition:UITableViewScrollPositionNone];
我发现表格没有滚动到所选行的原因是双重的。
因为我以编程方式选择行,并且使用的是 scrollPosition:UITableViewScrollPositionNone。 使用这个,表没有移动到所需的行。 我将其更改为 scrollPosition:UITableViewScrollPositionTop。
另一个问题是我在 ViewDidAppear 中重新加载表视图。 将其更改为 ViewDidLoad 在保持所选行外观方面有所不同。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.