I'm using a split-view controller. For iPad, when the app starts, I want to selected in the first item in master-view controller. So how can I do that in Swift? Thanks!
I solved this problem. I am writing to help others in the future.
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
}
}
}
Converted to 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)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With