【问题标题】:How to select default item in detail view for iPad? (in Split View Controller)如何在 iPad 的详细视图中选择默认项目? (在拆分视图控制器中)
【发布时间】:2015-12-03 12:16:08
【问题描述】:

我正在使用拆分视图控制器。对于 iPad,当应用程序启动时,我想在主视图控制器的第一项中进行选择。那么我怎样才能在 Swift 中做到这一点呢?谢谢!

【问题讨论】:

    标签: ios swift uisplitviewcontroller


    【解决方案1】:

    我解决了这个问题。我写信是为了将来帮助别人。

    func viewDidLoad() {
            //..
            let initialIndexPath = NSIndexPath(forRow: 0, inSection: 0)
            self.tableView.selectRowAtIndexPath(initialIndexPath, animated: true, scrollPosition:UITableViewScrollPosition.None)
    
            if UIDevice.currentDevice().userInterfaceIdiom == .Pad {
                self.performSegueWithIdentifier("ShowDetail", sender: initialIndexPath)
            }
    }
    
    func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
            if segue.identifier == "ShowDetail" {
                if sender!.isKindOfClass(UITableViewCell) {
                    if let indexPath = self.tableView.indexPathForCell(sender as! UITableViewCell) {
    
                        // Get row from selected row in tableview
    
                    }
                } else if sender!.isKindOfClass(NSIndexPath) {
                    let tableCell = self.tableView.cellForRowAtIndexPath(sender as! NSIndexPath)
                    let indexPath = self.tableView.indexPathForCell(tableCell!)
    
                    // Get row from sender, which is an NSIndexPath
                }
            }
    }
    

    【讨论】:

      【解决方案2】:

      转换为 Swift 3:

      let initialIndexPath = IndexPath(row: 0, section: 0)
      self.tableView.selectRow(at: initialIndexPath, animated: true, scrollPosition: UITableViewScrollPosition.none)
                  
      if UIDevice.current.userInterfaceIdiom == .pad {
          self.performSegue(withIdentifier: Constants.Segues.showDetail, sender: initialIndexPath)
      }
      

      【讨论】:

      • 这段代码应该去哪里?我的 NewsViewController 子类没有成员 tableView?
      【解决方案3】:

      在修复自动错误后转换为 Swift 5

      let initialIndexPath = NSIndexPath(row: 0, section: 0)
      self.tableView.selectRow(at: initialIndexPath as IndexPath, animated: true, scrollPosition:UITableView.ScrollPosition.none)
                  
      if UIDevice.current.userInterfaceIdiom == .pad {
          self.performSegue(withIdentifier: "ShowDetail", sender: initialIndexPath)
      }
      

      【讨论】:

        猜你喜欢
        • 2015-11-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-11-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多