gpt4 book ai didi

ios - 尝试在 viewWillAppear 中重新加载数据()

转载 作者:搜寻专家 更新时间:2023-11-01 06:11:33 27 4
gpt4 key购买 nike

我有一个 tabBarController,在其中一个选项卡中,我可以选择要放入“收藏夹”选项卡中的任何文档。

所以当我转到“收藏夹”选项卡时,应该会出现收藏的文档。

我在从 CoreData 中获取最喜欢的文档后调用重新加载:

override func viewWillAppear(_ animated: Bool) {

languageSelected = UserDefaults().string(forKey: "language")!

self.title = "favourites".localized(lang: languageSelected)

// Sets the search Bar in the navigationBar.
let search = UISearchController(searchResultsController: nil)
search.searchResultsUpdater = self
search.obscuresBackgroundDuringPresentation = false
search.searchBar.placeholder = "searchDocuments".localized(lang: languageSelected)
navigationItem.searchController = search
navigationItem.hidesSearchBarWhenScrolling = false

// Request the documents and reload the tableView.
fetchDocuments()

self.tableView.reloadData()

}

fetchDocuments()函数如下:

func fetchDocuments() {
print("fetchDocuments")
// We make the request to the context to get the documents we want.

do {

documentArray = try context.fetchMOs(requestedEntity, sortBy: requestedSortBy, predicate: requestedPredicate)
***print(documentArray) // To test it works ok.***
// Arrange the documentArray per year using the variable documentArrayPerSection.
let formatter = DateFormatter()
formatter.dateFormat = "yyyy"

for yearSection in IndexSections.sharedInstance.allSections[0].sections {

let documentsFilteredPerYear = documentArray.filter { document -> Bool in
return yearSection == formatter.string(from: document.date!)
}

documentArrayPerSection.append(documentsFilteredPerYear)

}

} catch {

print("Error fetching data from context \(error)")

}

}

从语句 print(documentArray) 我看到函数更新了内容。但是,tableView 中没有重新加载文档。

如果我关闭应用程序并再次打开它,它就会更新。

不知道我做错了什么!!!

最佳答案

问题是你总是追加到 documentArrayPerSection 但从不清除它所以我想数组总是变大但只是 tableView 的数据源请求的数组的开头正在被使用。我自己去过那里几次。

关于ios - 尝试在 viewWillAppear 中重新加载数据(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55735558/

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