我有一个 UITableView 有 3 个部分,每个部分有 3 个表格,我使用 3 个弹出窗口来填充每个部分的表格(公式、时间、步骤),此时一切正常
var sections : [String] = []
var buttons : [Int] = []
sections = [" Formula:", " Time:", " Steps: "]
buttons = [1,2,3]
var formula : [Formula] = []
var time : [Time] = []
var step : [Step] = []
var fetchReultFormula : NSFetchedResultsController<Formula>!
var fetchResultTime : NSFetchedResultsController<Time>!
var fetchResultStep : NSFetchedResultsController<Step>!
在代码的tableView部分:
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return sections[section]
}
func numberOfSections(in tableView: UITableView) -> Int {
return sections.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
switch section {
case 0:
return formula.count
case 1:
return time.count
default:
return step.count
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell :UITableViewCell = UITableViewCell(style: .subtitle, reuseIdentifier: "mycell")
switch (indexPath.section) {
case 0:
let result = formula[indexPath.row]
cell.textLabel?.text = result.ing
cell.detailTextLabel?.text = "\(result.cant) grs"
cell.imageView?.image = UIImage(named: result.type!)
case 1:
let result = [indexPath.row]
cell.textLabel?.text = result.name
cell.detailTextLabel?.text = "\(resultado.min) min"
cell.imageView?.image = UIImage(named: "time")
default:
let result = step[indexPath.row]
cell.textLabel?.text = result.desc
cell.detailTextLabel?.text = ""
cell.imageView?.image = UIImage(named: "check")
}
return cell
}
当我通过 popUpView 添加记录到任何表时,在关闭 popUpView 时如何将此记录添加到相应部分?
最佳答案
根据我们的意见,让我们与代表团一起去看看我们是否可以解决您的问题:
声明一个协议(protocol),您可以在另一个文件或您的ViewController
中执行:
该协议(protocol)有一个在 FormulaViewController
中添加数据后再次获取数据的方法
protocol DataFetcherDelegate {
func fetchData()
}
class ViewController: UIViewController {
然后从您的 ViewController
遵守此协议(protocol):
class ViewController: UIViewController, DataFetcherDelegate {
遵守协议(protocol)后,您必须在ViewController
内实现fetchData
方法,这是您要放置核心数据获取请求的地方日期数据:
func fetchData() {
print("fetchingData")
// write your data fetching code here, then make your arrays equal respectively again.
// once fetching and assigning data to arrays are complete do:
tableView.reloadData()
}
现在转到FormulaViewController
并添加一个delegate
变量,如下所示:
class FormulaViewController: UIViewController {
var delegate: DataFetcherDelegate?
返回到 ViewController
然后,无论你在哪里实例化 FormulaViewController
(我从你的评论中获得了下面的代码,你需要将 VC 转换为 FormulaViewController
也是如此,它有点更新,不要使用旧代码)并将 ViewController
分配为 FormulaViewController
的 delegate
:
let vc = storyboard!.instantiateViewController(withIdentifier: "formulaView") as! FormulaViewController
vc.delegate = self // assigning self as delegate of FormulaVC
self.present(vc, animated: true, completion: nil)
现在我们已经完成了ViewController
部分,我们现在将转到FormulaViewController
并执行以下操作:
在成功保存新数据后关闭 popupView 之前,现在我们要告诉我们的 delegate
(ViewController
) 再次获取核心数据,然后我们将关闭 FormulaViewController
:
//save your respective item to your core data, formula, time or step
// then for the sake of example, lets add a Formula:
let newFormula = Formula(/*with your core data context*/)
//save to your core data with context. like persistentContainer.viewContext.save(), I dont know how you implemented that.
delegate?.fetchData()
dismiss(animated: true, completion: nil)
现在希望一旦 FormulaViewController
被关闭,ViewController
的数据也会被更新。
关于ios - 如何将单元格添加到 UITableView 部分并保存核心数据信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54208754/