0

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 Answers2

0

you are missing thisatableView.reloadData()on yourviewDidLoadorviewDidAppear

pesch
  • 1,976
  • 2
  • 17
  • 29
  • table view is displaying but the problem was in iPad layout the value which I was selected was not passing to the details view controller –  May 10 '17 at 10:23
  • Have you checked if you are you setting the label's text property before viewDidLoad? – pesch May 10 '17 at 10:25
  • are u having any one of the split view controller project done previously ? –  May 10 '17 at 10:30
  • I don't understand why you are doing `if let path = tableView.indexPathForSelectedRow` at the end of your prepareForSegue. Do you have the segue connected from your tableViewCell to your destinationVC? – pesch May 10 '17 at 10:33
  • yes to send the selected index to display on the details view controller –  May 10 '17 at 10:39
0

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
rameez
  • 374
  • 2
  • 11
  • later on I changed and added now problem is when I run the app on iPad it is not passing value from master to details and when I rotate to landscape it is not working completely when I rotate it later it was working fine and passing all the values –  May 11 '17 at 12:27
  • it was passing the value when I rotate two times the iPad screen –  May 12 '17 at 05:05
  • i cannot see your didSelectIndexPath method. – rameez May 12 '17 at 06:45
  • see in my master view controller if let path = tableView.indexPathForSelectedRow { detail.selectedIndex = path.row + 1 } –  May 12 '17 at 07:16
  • if possible can you please send me ur mail id so that u can check and tell me what's wrong in my project ? –  May 12 '17 at 07:27