gpt4 book ai didi

swift - 横向启动时主视图单元格标签为空白

转载 作者:行者123 更新时间:2023-11-30 12:21:00 25 4
gpt4 key购买 nike

当以 iPad 2 模拟的横向方向启动时,默认主详细信息 View 的主视图中的单元格标签为空白。如果我在其 Controller 的 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 View 设置的人来说,是的,Master View 确实以横向方式开始,并填充了它设置为显示的任何标签。我的问题是,我用来填充标签的数组是从 View 异步构造的,并且在加载 View 时该数组尚未准备好。我通过设置一个 NSNotification 来解决这个问题,当数组完成发现我的 UIDocuments 时,通知我的主视图。 Andrew Bancroft 的博客 (https://www.andrewcbancroft.com/2014/10/08/fundamentals-of-nsnotificationcenter-in-swift/) 在这方面非常有帮助。

关于swift - 横向启动时主视图单元格标签为空白,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44790819/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com