簡體   English   中英

橫向啟動時,主視圖單元格標簽為空白

[英]Master View Cell Labels Blank When Starting In Landscape

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

}

對於像我一樣對默認Xcode Master-Detail視圖設置不熟悉的人,是的,Master視圖的確以橫向顯示開始,並填充了要設置為顯示的任何標簽。 我的問題是,我用來填充標簽的數組是從視圖異步構造的,而在加載視圖時該數組尚未准備就緒。 我通過設置NSNotification來解決此問題,該NSNotification在陣列完成發現UIDocuments時告訴我的主視圖。 在這方面,安德魯·班克羅夫特(Andrew Bancroft)的博客( https://www.andrewcbancroft.com/2014/10/08/fundamentals-of-nsnotificationcenter-in-swift/ )非常有用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM