0

The cell labels are blank in the master view of a default Master Detail View when starting in landscape orientation of an iPad 2 simulation. If I reload the master view in its controller's viewWillAppear function, everything is as it should be only after turning into portrait and back into landscape. I can't figure out what I am missing despite several hours of searching for help and trying to tableView.reloadData() in various places.

This is a UIDocument app and I have not yet implemented iCloud, although I have the code ready to go. Thus far, it just needs to fetch the local document URLs, file names, and display names (?) into an array from which the master view cell labels are created.

Here is most of the MasterViewController class:

class MasterViewController: UITableViewController, DetailViewControllerDelegate {

private var detailViewController: DetailViewController? = nil
//    var objects = [AnyObject]()
internal lazy var notesController = NotesController()

internal override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view, typically from a nib.
    print("viewDidLoad")

    // determine preferred storage location for documents
    notesController.documentsInCloud = false

    // discover documents
    notesController.discoverDocuments()
//        tableView.reloadData()

    navigationItem.leftBarButtonItem = editButtonItem()

    if let split = splitViewController {
        let controllers = split.viewControllers
        detailViewController =
            (controllers[controllers.count-1] as! UINavigationController
                ).topViewController as? DetailViewController
        detailViewController!.delegate = self
    }
}

internal override func viewWillAppear(animated: Bool) {
    print("viewWillAppear")
    clearsSelectionOnViewWillAppear = splitViewController!.collapsed
    super.viewWillAppear(animated)

    tableView.reloadData()
}

// MARK: - Segues

internal override func prepareForSegue(segue: UIStoryboardSegue,
                              sender: AnyObject?) {
    print("prepareForSegue")
    if segue.identifier == "showDetail" {
        if let indexPath = tableView.indexPathForSelectedRow {
            let controller =
                (segue.destinationViewController as! UINavigationController
                    ).topViewController as! DetailViewController
            let URL = notesController.notes.array[indexPath.row].URL
            controller.delegate = self
            controller.detailItem = Note(fileURL: URL)
            controller.selectedItemIndex = indexPath.row
            controller.navigationItem.leftBarButtonItem =
                splitViewController?.displayModeButtonItem()
            controller.navigationItem.leftItemsSupplementBackButton = true
            splitViewController?.toggleMasterView()
        } else {
            let controller =
                (segue.destinationViewController as! UINavigationController
                    ).topViewController as! DetailViewController
            controller.delegate = self
            controller.configureView()
            controller.navigationItem.leftBarButtonItem =
                splitViewController?.displayModeButtonItem()
            controller.navigationItem.leftItemsSupplementBackButton = true
        }
    }
}

// MARK: - Table View

internal override func numberOfSectionsInTableView(tableView: UITableView)
    -> Int {
    return 1
}

internal override func tableView(tableView: UITableView,
                        numberOfRowsInSection section: Int) -> Int {
    return notesController.notes.array.count
}

internal override func tableView(tableView: UITableView,
                        cellForRowAtIndexPath indexPath: NSIndexPath)
    -> UITableViewCell {
        print("cellForRowAtIndexPath")
        let cell =
            tableView.dequeueReusableCellWithIdentifier(
                "Cell",
                forIndexPath: indexPath)
        let fileRepresentation = notesController.notes.array[indexPath.row]
        if let title = fileRepresentation.displayName {
            cell.textLabel?.text = title
        } else {
            cell.textLabel?.text = fileRepresentation.fileName
        }
        return cell
}

internal override func tableView(tableView: UITableView,
                        canEditRowAtIndexPath indexPath: NSIndexPath)
    -> Bool {
    // Return false if you do not want the specified item to be editable.
    return true
}

internal override func tableView(
    tableView: UITableView,
    commitEditingStyle
    editingStyle: UITableViewCellEditingStyle,
    forRowAtIndexPath indexPath: NSIndexPath) {
    print("commitEditingStyle")
    if editingStyle == .Delete {
        let fileManager = NSFileManager.defaultManager()
        let fileRepresentation = notesController.notes.array[indexPath.row]
        let URL = fileRepresentation.URL
        do {
            try fileManager.removeItemAtURL(URL);
            notesController.notes.delete(fileRepresentation);
            tableView.deleteRowsAtIndexPaths([indexPath],
                                             withRowAnimation: .Fade);
            performSegueWithIdentifier("showDetail", sender: self)
        } catch let error as NSError {
            print(error.localizedDescription)
        }
    } // else if editingStyle == .Insert {
        // Create a new instance of the appropriate class, insert it into
        // the array, and add a new row to the table view.
    // }
}

// MARK: - Delegate Functions

internal func reloadMasterViewData(sender: DetailViewController) {
    tableView.reloadData()
}

}
michaelgill1969
  • 131
  • 2
  • 9

1 Answers1

0

For those who, like me, are new to the default Xcode Master-Detail view setup, yes, the Master view does start in landscape orientation populated with whatever labels it is set up to display. My problem was that the array I am using to populate the labels is constructed asynchronously from the views, and that array wasn't ready when the view loaded. I fixed this by setting up an NSNotification that told my master view when the array was finished discovering my UIDocuments. Andrew Bancroft's blog (https://www.andrewcbancroft.com/2014/10/08/fundamentals-of-nsnotificationcenter-in-swift/) was very helpful in that regard.

michaelgill1969
  • 131
  • 2
  • 9