Project not working - TableViewController - No Errors

Hey, i started learning swift for iOS App. Now i wanted to learn about core data. I don't get any errors when compiling. I "hard coded" some test datas for the tableview. But it just shows the table without "entry#(number)" as title and subtitle "another entry". This Project was a video about CoreData and in the video everything worked. Why doesn't it work on my Mac? Thanks in advance



import UIKit
import CoreData


class ListTableViewController: UITableViewController {
   
    let mgdContext = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
   
    var listenArray = [Liste]() {
        didSet {
            tableView.reloadData()
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.reloadData()
        if let lists = loadLists() {
            listenArray = lists
        }
    }
    @IBAction func addListTapped(_ sender: Any) {
        newList()
        if let lists = loadLists() {
            listenArray = lists
        }
    }
   
    // MARK: - Table view data source



}

extension ListTableViewController {
    func newList() {
        let newList = NSEntityDescription.insertNewObject(forEntityName: "Liste", into: mgdContext) as! Liste
        newList.name = "entry#\(listenArray.count + 1)"
        newList.subtitle = "another entry"
       
        do {
            try mgdContext.save()
        } catch {
            print(error.localizedDescription)
        }
    }
   
    func loadLists() -> [Liste]? {
        let request: NSFetchRequest = Liste.fetchRequest()
       
        do {
            let result = try mgdContext.fetch(request)
            return result
        } catch {
            print(error.localizedDescription)
        }
        return nil
    }
}
extension ListTableViewController {
    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return listenArray.count
    }
   
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "listCell", for: indexPath)
       
        let liste = listenArray[indexPath.row]
       
        cell.textLabel?.text = liste.name
        cell.detailTextLabel?.text = liste.subtitle
       
        return cell
    }
   
}

Accepted Reply

You wrote


    override func numberOfSections(in tableView: UITableView) -> Int { 
        // #warning Incomplete implementation, return the number of sections 
        return listenArray.count 
    }


Are you sure listenArray.count is the number of sections and not the number of rows in section ?


probably you should have (if only one section)

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


And if you have only one section, no need to override numberOfSections.

Replies

You wrote


    override func numberOfSections(in tableView: UITableView) -> Int { 
        // #warning Incomplete implementation, return the number of sections 
        return listenArray.count 
    }


Are you sure listenArray.count is the number of sections and not the number of rows in section ?


probably you should have (if only one section)

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


And if you have only one section, no need to override numberOfSections.

thank you very much. Wastet so much time with that😮. I need to override number of rows in section not number of sections.