我有一個 UIView,它顯示一些資訊,例如用戶名等,包括所有從我的資料庫中提取的物件串列。這作業正常。
但是,我現在有一個 ViewController,它顯示在當前 ViewController 之上。在這個呈現的 ViewController 中,我將資料添加到我的資料庫中。關閉該視圖時,我希望原始 ViewController 將其所有內容更新為最新的。
現在,我所有的視圖都在 ViewDidLoad 中進行布局,這意味著它們只會真正加載一次,以后不會重新加載。我已經設法通過呼叫來更新布局self.view.layoutIfNeeded(),但如果我理解正確,這只會更新約束。當然,我可以呼叫我原來的視圖控制器的一個新的 init。這將使它重新加載,但我想避免這種情況。
我的另一個想法是在 ViewWillAppear 中設定我的所有內容,這可能會在我的視圖控制器即將可見的任何時候更新。但是,我不知道該怎么做。我可以將所有設定代碼移動到 viewWillAppear 嗎?這有什么缺點嗎?
TLDR:有沒有辦法用新元素更新堆疊視圖,而無需通過 ViewWillAppear 重新加載完整的 ViewController?
uj5u.com熱心網友回復:
UITableView 元素與資料庫資料一起作業非常順利。如果您在第一個視圖控制器的 viewDidLoad 中從資料庫中獲取資料,并將其存盤在一個陣列中,則 UITableView(如果您正確設定了它的 dataSource)將使用來自第二個視圖控制器的新值自動填充表。使用這種方法,根本不需要使用 ViewWillAppear。
聽起來到目前為止,您正在使用視圖(在 VStack 內)?顯示資料庫中的單個物件。如果您想保留您在視圖中使用的任何自定義樣式/布局,可以通過定義 UITableViewCell 的自定義子類并選擇“同時創建 XIB 檔案”選項來完成。XIB 檔案允許您自定義 UITableView 中單元格的外觀。
這是一個簡單的示例,顯示第一個視圖控制器中的資料庫值自動更新。我沒有包含自定義 XIB 檔案(這些都是默認的 UITableViewCells),以使其保持精簡。
第一視角控制器
import UIKit
import CoreData
class ViewController: UIViewController {
@IBOutlet weak var dataTable: UITableView!
var tableRows: [DataItem] = []
func loadData() {
let request: NSFetchRequest<DataItem> = DataItem.fetchRequest()
do {
tableRows = try Global_Context.fetch(request)
} catch {
print("Error loading data: \(error)")
}
}
override func viewDidLoad() {
super.viewDidLoad()
dataTable.dataSource = self
loadData()
}
@IBAction func goForward(_ sender: UIButton) {
self.performSegue(withIdentifier: "toSecond", sender: self)
}
}
extension ViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return tableRows.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "dataTableCell", for: indexPath)
cell.textLabel?.text = tableRows[indexPath.row].name
return cell
}
}
let Global_Context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
func saveContext () {
if Global_Context.hasChanges {
do {
try Global_Context.save()
} catch {
let nserror = error as NSError
print("Error saving database context: \(nserror), \(nserror.userInfo)")
}
}
}
第二視圖控制器:
import UIKit
import CoreData
class AddViewController: UIViewController {
@IBOutlet weak var itemEntry: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
itemEntry.delegate = self
}
@IBAction func addNewItem(_ sender: UIButton) {
let newDataItem = DataItem(context: Global_Context)
newDataItem.name = itemEntry.text
saveContext()
}
@IBAction func goBack(_ sender: UIButton) {
self.performSegue(withIdentifier: "toFirst", sender: self)
}
}
extension AddViewController: UITextFieldDelegate {
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.endEditing(true)
return true
}
}
主故事板:

Once you set up your view controller as a UITableViewDataSource (as in the example code), the table view should make things simpler by eliminating any need to manually manage individual Views.
Is this the functionality you were looking for? (Note about the example: it was set up in Xcode with "Use Core Data" enabled.)
Here is a link to the official documentation: https://developer.apple.com/documentation/uikit/uitableview
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/454400.html
