在此拆分视图中,如果我拖动它正在显示,并且如果我选择一个索引,它不会显示在标签上
class ListTableViewController: UITableViewController {
let names = ["One","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten"]
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return names.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CellIdentifier", for: indexPath)
cell.isSelected = true
cell.textLabel?.text = names[indexPath.row]
return cell
}
// MARK:- Storyboard segue
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if (segue.identifier == "ShowDetailIdentifier") {
var detail: DetailsViewController
if let navigationController = segue.destination as? UINavigationController {
detail = navigationController.topViewController as! DetailsViewController
} else {
detail = segue.destination as! DetailsViewController
}
if let path = tableView.indexPathForSelectedRow {
detail.selectedIndex = path.row + 1
}
}
}
主视图控制器中的代码
@IBOutlet weak var numberLabel: UILabel!
var selectedIndex:Int = 1
override func viewDidLoad() {
super.viewDidLoad()
numberLabel?.text = "(selectedIndex)"
print(selectedIndex)
if splitViewController?.responds(to: #selector(getter: UISplitViewController.displayModeButtonItem)) == true {
navigationItem.leftBarButtonItem = splitViewController?.displayModeButtonItem
navigationItem.leftItemsSupplementBackButton = true
}
详细信息视图控制器中的代码
class SplitViewController: UISplitViewController,UISplitViewControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
splitViewController?.preferredDisplayMode = .primaryOverlay
splitViewController?.delegate = self
// Do any additional setup after loading the view.
}
func splitViewController(_ splitViewController: UISplitViewController,
collapseSecondary secondaryViewController: UIViewController,
onto primaryViewController: UIViewController) -> Bool {
return true
}
拆分视图控制器的代码
你缺少这个a
tableView.reloadData(( on your
viewDidLoad or
viewDidAppear
我看不出你在哪里初始化拆分视图控制器,你需要传递 TableViewController 和 detailViewController。您需要在继承UISplitViewController的类的viewDidLoad中传递它
self.viewControllers = [masterNav, detail]
并且要始终显示 SplitViewController 您需要这个
self.displayMode = .allVisible