【问题标题】:How can i add cell to UITableView Section with core data saved info如何将单元格添加到 UITableView 部分并保存核心数据信息
【发布时间】:2019-06-10 01:10:53
【问题描述】:

我有一个包含 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 时将此记录添加到相应部分?

【问题讨论】:

    标签: ios swift uitableview core-data


    【解决方案1】:

    由于我们的 cmets,让我们与委托一起去看看我们是否可以解决您的问题:

    声明一个协议,你可以在另一个文件或ViewController

    这个协议有一个方法可以在你在FormulaViewController中添加数据后再次获取数据

    protocol DataFetcherDelegate {
        func fetchData()
    }
    
    
    class ViewController: UIViewController {
    

    然后从你的ViewController遵守这个协议:

    class ViewController: UIViewController, DataFetcherDelegate {
    

    在你遵守协议之后,你将不得不在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,它有点更新不要' t 使用您的旧代码)并将ViewController 分配为FormulaViewControllerdelegate

    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 的数据也会更新。

    【讨论】:

    • 感谢您的帮助。我是 swift 的新手,我使用 let vc 调用弹出窗口:UIViewController = storyboard!.instantiateViewController(withIdentifier: "formulaView") 和 self.present(vc, animated: true, completion: nil) 来自 ViewController.swift 并在 FormulaViewController 上关闭。保存数据后迅速。我如何知道 ViewController.swift 上哪个表有新记录,以及如何在 tableView 上添加一条新记录。
    • 您需要使用委托模式,使用该模式您将能够从 FormulaViewController 触发基本 ViewController 的某些功能。做一些研究如何做到这一点,如果你不能。我会尽力帮助你。
    • 请帮帮我,我真的是 swift 新手,我会非常感谢
    • 在我们进行委托之前,您可以尝试在 ViewController 的 viewWillAppear 中获取 coreData 吗?这样,您将数据保存在 FormulaViewController 中,然后将其关闭。当您关闭它时,ViewController 的视图将再次出现,然后您应该获取核心数据,并且在获取完成并且所有数据数组都部署了最新数据后,请执行tableView.reloadData()
    • 我做到了,事实上,当 viewWillAppear 执行但只有第一次发生时,我放了一条打印语句向控制台日志发送消息,当我关闭弹出 viewController 时没有任何反应。 :(
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-30
    • 1970-01-01
    • 1970-01-01
    • 2011-12-22
    • 1970-01-01
    相关资源
    最近更新 更多