TheLionCubOfCintra
TheLionCubOfCintra

Reputation: 13

tableView reloadData doesn't work, delegate methods

I am trying to create new category in 1 view controller (AddCategoryViewController) and show it in table view controller (CategoryViewController). But there's an issue with reloading data.

New category item shows only after turning on and off the app, even when there is tableView.reloadData().

I tried to change the title of navigation in addButtonPressed function and the title changes immediately.

When I was using UIAlertView to add data, tableView.reloadData() worked. So I guess it's something with 2 view controllers and delegate methods?

Thanks for your help <3

show item:

import UIKit
import CoreData

class CategoryViewController: UITableViewController {

    @IBOutlet weak var navigation: UINavigationItem!

    var categoryArray = [Category]()
    let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext

    override func viewDidLoad() {
        super.viewDidLoad()

        loadCategory()
    }


    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return categoryArray.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell(style: .default, reuseIdentifier: "CategoryItemCell")

        cell.textLabel?.text = categoryArray[indexPath.row].name

        if let randomColor = categoryArray[indexPath.row].color {
            cell.textLabel?.textColor = UIColor(hex: randomColor)
        }

        return cell
    }


    // MARK: - Table view data source
    @IBAction func addPressed(_ sender: UIBarButtonItem) {

        let addCategoryVC = storyboard?.instantiateViewController(withIdentifier: "AddCategoryViewController") as! AddCategoryViewController
        addCategoryVC.delegate = self
        present(addCategoryVC, animated: true, completion: nil)
    }


    // MARK: - CoreData methods

    func saveCategory() {

        do {
            try context.save()

        } catch {
            print("Save error: \(error)")
        }

        tableView.reloadData()

    }

    func loadCategory(with request: NSFetchRequest<Category> = Category.fetchRequest()) {
        do {
            categoryArray = try context.fetch(request)
        } catch {
            print("Load error: \(error)")
        }

        tableView.reloadData()
    }

    func addCategory(name: String, description: String) {

        let newCategory = Category(context: context.self)
        newCategory.name = name
        newCategory.descriptionOfCategory = description
        newCategory.color = UIColor.random().toHex
        saveCategory()

        print("name form func: \(name)")
        print("description from func: \(description)")

    }

}

// MARK: AddCateogry delegate methods

extension CategoryViewController: AddCategoryDelegate {

    func addButtonPressed(name: String, description: String) {
        addCategory(name: name, description: description)
        navigation.title = "I have changed!"
    }
}

Add item:

import UIKit

protocol AddCategoryDelegate {
    func addButtonPressed(name: String, description: String)
}

class AddCategoryViewController: UIViewController {

    var delegate : AddCategoryDelegate!

    @IBOutlet weak var nameTextField: UITextField!
    @IBOutlet weak var descriptionTextField: UITextField!

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBAction func addCategoryButtonPressed(_ sender: UIButton) {
        delegate.addButtonPressed(name: nameTextField.text!, description: descriptionTextField.text!)
        dismiss(animated: true, completion: nil)
    }

}

Upvotes: 0

Views: 423

Answers (1)

Shehata Gamal
Shehata Gamal

Reputation: 100503

You only save the category to coredata inside addCategory , but you have to add the item to the array also , or call loadCategory before tableView.reloadData() inside saveCategory

Upvotes: 1

Related Questions