【问题标题】:Automatically set focus to first cell of UITableView when view loads视图加载时自动将焦点设置到 UITableView 的第一个单元格
【发布时间】:2023-04-08 11:22:01
【问题描述】:

我一直在尝试制作一个应用程序,当视图加载时,焦点会自动转到 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;
    }
}

在视图中确实加载了

        self.viewToFocus = myTableView

但这并没有奏效。

【问题讨论】:

  • 视图是否只包含表格视图?或者它也包含其他视图?
  • @Mr.UB 是的,它也有collectionview。当用户关注uitableview 中的项目时,我正在填充colllectionview。所以默认情况下,第一个单元格应该集中在 tableview 中!!!
  • 当你说prefferedFocusView时,你的意思是preferredFocusedView吗?
  • read the whole of this discussion 为问题添加紧急乞求。

标签: swift uitableview tvos


【解决方案1】:

我可以通过在viewDidLoad 中设置tableView.remembersLastFocusedIndexPath = true 来实现这一点,然后我实现了首选焦点的委托方法:

func indexPathForPreferredFocusedView(in tableView: UITableView) -> IndexPath? {
   return NSIndexPath(forItem: 0, inSection: 0)
}

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.

斯威夫特 5

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
    myTableView.remembersLastFocusedIndexPath = true
}

然后在你的委托中

func indexPathForPreferredFocusedView(in tableView: UITableView) -> IndexPath? {
    return IndexPath(row: 0, section: 0)
}

【讨论】:

    【解决方案2】:

    在 tableView:WillDisplayCell 方法中添加以下代码

     [cell setSelected:YES animated:NO];
    

    第一个单元格

    【讨论】:

      【解决方案3】:

      您可以设置滚动位置自动转到顶部单元格。

      let topCell:NSIndexPath = NSIndexPath(forRow: 0, inSection: 0)
      self.tableView.scrollToRowAtIndexPath(topCell, atScrollPosition: UITableViewScrollPosition.None, animated: true)
      

      我不确定这是否与preferredFocusedView虽然相同。

      【讨论】:

      • @Saty 抱歉,我从来没有注意到它是针对 tvos 的。希望你能把它整理好
      【解决方案4】:

      将以下代码 sn-p 添加到 ViewController 类的实现中,并调用 tableview 委托:

      func tableView(tableView: UITableView, canFocusRowAtIndexPath indexPath: NSIndexPath) -> Bool {
      if indexPath.row == 0 {
          return true
      }
      return false
      }
      

      【讨论】:

      • 这将允许他聚焦除第一个项目之外的所有项目!
      【解决方案5】:
      extension UITableView {
      
          func scrollToTop(section: Int) {
              let rows = self.numberOfRows(inSection: section)
      
              if rows > 0 {
                  DispatchQueue.main.async {
                      let indexPath = IndexPath(row: rows - 1, section: section)
                      self.scrollToRow(at: indexPath, at: .top, animated: true)
                  }
              }
          }
      }
      

      如果只有一个部分,则将部分替换为 0

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-10-04
        • 1970-01-01
        • 2018-03-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多