in iPad screen it is not displaying split view

139 Views Asked by At

In this split view was not displaying on the iPad screen if I drag it was displaying and if I select an index it is not displaying on the label

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

the code in master view controller

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

the code in details view controller

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
    }

the code for split view controller

2

There are 2 best solutions below

5
pesch On

you are missing thisatableView.reloadData()on yourviewDidLoadorviewDidAppear

5
rameez On

I can't see where are you initializing the splitviewcontroller you need to pass the TableViewController, and detailViewController. You need to pass it in the viewDidLoad of the class inheriting form UISplitViewController

self.viewControllers = [masterNav, detail]

and to always show splitviewcontroller you need this

self.displayMode = .allVisible