我有3个部分的UITableView,每个部分有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时如何将该记录添加到各个部分?

最佳答案

根据我们的评论,让我们与代表团一起看看我们是否可以解决您的问题:

声明协议,您可以在另一个文件或ViewController中进行操作:

FormulaViewController中添加数据后,此协议具有一种重新获取数据的方法

protocol DataFetcherDelegate {
    func fetchData()
}


class ViewController: UIViewController {


然后从您的ViewController遵循以下协议:

class ViewController: UIViewController, DataFetcherDelegate {


遵循协议后,您将必须在fetchData内实现ViewController方法,在这里您将放置核心数据获取请求以获取最新数据:

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分配为delegateFormulaViewController

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之前,现在我们要告诉delegateViewController)再次获取核心数据,然后我们将关闭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/

10-14 23:22
查看更多