我有一个应用程序,我得到的商店和境界检索数据。 我创建了一个包含一个文本框的数据输入一个弹出式窗口,我还创建了我使用重新加载tableview中点击后保存按钮,但现在的问题是在点击按钮时,该表没有得到重新加载一个回调函数。
AddCategory.swift
//Callback:- Property that holds a function
var doneSaving: (() -> ())?
@IBAction func saveActionBtn(_ sender: Any) {
CategoryFunctions.instance.createCategory(name: name, color: color, isCompleted: false)
if let doneSaving = doneSaving {
doneSaving()
}
dismiss(animated: true, completion: nil)
}
category.swift
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if (segue.identifier == Constants.TO_ADD_CATEGORY_VC) {
let popUp = segue.destination as! AddCategoryVC
//2 Callback implemented
popUp.doneSaving = {[weak self] in
self?.tableView.reloadData()
}
}
}
我在做什么错事或者我能做些什么来解决这个问题?
func createCategory(name: String, color: String, isCompleted: Bool) -> Void {
let category = CategoryModel()
category.name = name
category.color = color
category.isCompleted = false
DBManager.instance.addData(object: category)
}
//MARK:- Read Category
func readCategory(completion: @escaping CompletionHandler) -> Void {
DBManager.instance.getDataFromDB().forEach({ (category) in
let category = CategoryModel()
Data.categoryModels.append(category)
})
}
func getDataFromDB() -> Results<CategoryModel> {
let categoryArray: Results<CategoryModel> = database.objects(CategoryModel.self)
return categoryArray
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: Constants.CATEGORY_CELL) as! CategoryCell
let category = categoryArray[indexPath.row]
cell.setup(categoryModel: category)
return cell
}