簡體   English   中英

如何在 iPad 的詳細視圖中選擇默認項目? (在拆分視圖控制器中)

[英]How to select default item in detail view for iPad? (in Split View Controller)

我正在使用拆分視圖控制器。 對於 iPad,當應用程序啟動時,我想在主視圖控制器的第一項中選擇。 那么我如何在 Swift 中做到這一點呢? 謝謝!

我解決了這個問題。 我寫信是為了在未來幫助他人。

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
            }
        }
}

轉換為 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)
}

修復自動錯誤后轉換為 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)
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM