James Thomson
James Thomson

Reputation: 15

Data not displaying on TableView from CoreData array

The data is not displaying from an array loaded from CoreData.

I have tried print statements to see if information is being displayed and looked at code from another ViewController in the navigation controller set up.

Its a small but simple programme. This View Controller contains the categories prior to segueing to another view controller that contains the data/todo list within that category.

//
//  CategoryViewController.swift
//  Todoey
//
//  Created by James Thomson on 06/01/2019.
//  Copyright © 2019 James Thomson. All rights reserved.
//

import UIKit
import CoreData


class CategoryViewController: UITableViewController {

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

override func viewDidLoad() {
    loadCategory()
    self.tableView.reloadData()
    super.viewDidLoad()
}

//MARK: - TableView Data Source Methods
override func numberOfSections(in tableView: UITableView) -> Int {
    return categories.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "categoryCell", for: indexPath)
    cell.textLabel?.text = categories[indexPath.row].name

    return cell
}

//MARK: - Add New Categories

@IBAction func addBtnPressed(_ sender: UIBarButtonItem) {
    var textField = UITextField()
    let alert = UIAlertController(title: "Add Category", message:"", preferredStyle: .alert)

    let action = UIAlertAction(title: "Add", style: .default) { (action) in // THIS IS A COMPLETION HANDLER
        let newCategory = Category(context: self.context)
        newCategory.name = textField.text!

        self.categories.append(newCategory)
        self.saveCategory()

        DispatchQueue.main.async {
            self.tableView.reloadData()
        }

    }

    alert.addTextField { (alertTextField) in
        alertTextField.placeholder = "Create Category"
        textField = alertTextField
    }
    alert.addAction(action)
    present(alert, animated: true, completion: nil)

    print(categories)
}


//MARK: - TableView Delegate Methods
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

}

//MARK: - Data Manipulation Methods (SAVE & LOAD)

func saveCategory() {
    do {
        try context.save()
    } catch {
        print("Save Category Error: \(error)")
    }

    DispatchQueue.main.async { self.tableView.reloadData() }
}

func loadCategory(with request: NSFetchRequest<Category> = Category.fetchRequest()) {
    do {
        categories = try context.fetch(request)
    } catch {
        print("Load Category Error: \(error)")
    }
    DispatchQueue.main.async { self.tableView.reloadData() }
}

}

The table is currently showing blank despite their being data in the array.

Upvotes: 0

Views: 33

Answers (1)

James Thomson
James Thomson

Reputation: 15

I've done a classic beginners mistake and jumped the gun in the de-bug.

THIS

override func numberOfSections(in tableView: UITableView) -> Int {
    return categories.count
}

SHOULD BE

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

Upvotes: 0

Related Questions