gpt4 book ai didi

swift - viewDidLoad 之前和之后的区别

转载 作者:IT王子 更新时间:2023-10-29 05:27:45 24 4
gpt4 key购买 nike

我尝试将 searchBar 设置为 viewDidLoad 内的 tableHeaderView:

 override func viewDidLoad() {
super.viewDidLoad()


// SearchController initializiation
self.searchController = UISearchController.init(searchResultsController: nil)
self.searchController.delegate = self
self.searchController.searchBar.delegate = self
self.searchController.searchBar.sizeToFit()
self.searchController.searchResultsUpdater = self
self.searchController.searchBar.barTintColor = UIColor.white
self.searchController.searchBar.keyboardAppearance = .default
self.searchController.searchBar.backgroundColor = UIColor.white
self.searchController.hidesNavigationBarDuringPresentation = true
self.searchController.obscuresBackgroundDuringPresentation = false


self.tableView.tableHeaderView = self.searchController.searchBar
self.definesPresentationContext = true

self.fetch()
self.tableView.reloadData()

}

这是我的 fetch() 函数:

func fetch() {
let fetchRequest:NSFetchRequest<Phone> = Phone.fetchRequest()
fetchRequest.sortDescriptors = [NSSortDescriptor.init(key: "header", ascending: true), NSSortDescriptor.init(key: "date", ascending: true)]

self.fetchedResultsController = NSFetchedResultsController.init(fetchRequest: fetchRequest, managedObjectContext: self.managedObjectContext, sectionNameKeyPath: "header", cacheName: nil)
self.fetchedResultsController.delegate = self

do {
try self.fetchedResultsController.performFetch()
self.tableView.reloadData()
} catch {}

}

但我不明白它们是行不通的。 Xcode 崩溃了,实际上什么也没有发生。然后我尝试更改 viewDidLoad 方法中的某些内容:

override func viewDidLoad() {
super.viewDidLoad()

self.fetch()
self.tableView.reloadData()

// SearchController initializiation
self.searchController = UISearchController.init(searchResultsController: nil)
self.searchController.delegate = self
self.searchController.searchBar.delegate = self
self.searchController.searchBar.sizeToFit()
self.searchController.searchResultsUpdater = self
self.searchController.searchBar.barTintColor = UIColor.white
self.searchController.searchBar.keyboardAppearance = .default
self.searchController.searchBar.backgroundColor = UIColor.white
self.searchController.hidesNavigationBarDuringPresentation = true
self.searchController.obscuresBackgroundDuringPresentation = false


self.tableView.tableHeaderView = self.searchController.searchBar
self.definesPresentationContext = true



}

成功了!工作正常。我不明白有什么区别?

最佳答案

您应该尝试从 viewDidLoad() 方法中删除 self.tableView.reloadData(),因为数据甚至没有加载到重新加载。

关于swift - viewDidLoad 之前和之后的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43268763/

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