Sat*_*aty 0 uitableview swift tvos
我一直在尝试创建一个应用程序,当视图加载时,焦点自动转到UITableview的第一个单元格.
我尝试过使用UIFocusGuide,然后在prefferedFocusView()方法中返回tableView,但是没有用.
然后我写了这段代码
var viewToFocus: UIView? = nil {
didSet {
if viewToFocus != nil {
print("called1 ")
self.setNeedsFocusUpdate();
self.updateFocusIfNeeded();
}
}
}
override weak var preferredFocusedView: UIView? {
if viewToFocus != nil {
print("called2 ")
return viewToFocus;
} else {
return super.preferredFocusedView;
}
}
Run Code Online (Sandbox Code Playgroud)
在视图中确实加载
self.viewToFocus = myTableView
Run Code Online (Sandbox Code Playgroud)
但这并没有奏效.
我能够通过设置来得到这个tableView.remembersLastFocusedIndexPath = true的viewDidLoad话,我实施了首选焦点委托方法:
func indexPathForPreferredFocusedView(in tableView: UITableView) -> IndexPath? {
return NSIndexPath(forItem: 0, inSection: 0)
}
Run Code Online (Sandbox Code Playgroud)
当remembersLastFocusedIndexPath-If YES, when focusing on a table view the last focused index path is focused automatically. If the table view has never been focused, then the preferred focused index path is used.