在景观中开始时,主视图单元格标签为空白

时间:2017-06-27 22:17:41

标签: swift xcode7.3 uidocument ios9.3

在iPad 2模拟的横向方向启动时,单元格标签在默认主详细信息视图的主视图中为空白。如果我在其控制器的viewWillAppear函数中重新加载主视图,那么只有在变成肖像并返回到横向之后,一切都应该如此。尽管花了几个小时寻找帮助并尝试在各个地方使用tableView.reloadData(),但我无法弄清楚我错过了什么。

这是一个UIDocument应用程序,我还没有实现iCloud,虽然我已经准备好了代码。到目前为止,它只需要将本地文档URL,文件名和显示名称(?)提取到一个从中创建主视图单元格标签的数组中。

以下是MasterViewController类的大部分内容:

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

}

1 个答案:

答案 0 :(得分:0)

对于那些像我一样不熟悉默认Xcode Master-Detail视图设置的人来说,是的,Master视图确实以横向方式开始,填充了设置显示的任何标签。我的问题是我用来填充标签的数组是从视图中异步构造的,并且在加载视图时该数组还没有准备好。我通过设置一个NSNotification来解决这个问题,该NSNotification在数组完成发现我的UIDocuments时告诉我的主视图。 Andrew Bancroft的博客(https://www.andrewcbancroft.com/2014/10/08/fundamentals-of-nsnotificationcenter-in-swift/)在这方面非常有帮助。