'NSTableView not appearing at all

I've just started working on my first project for macOS and am having trouble setting up a NSTableView. When I run it the window will appear but there is nothing in it. I've made sure all the objects have the correct class in the identity inspector and can't seem to find what I'm doing wrong.

The goal of the app is to make a notes app. I want a tableView which displays the titles of all the notes in the database, in a single column, so when you click on the cell the note will then be displayed in the rest of the window.

Here's the code:

import Foundation
import AppKit
import SQLite

class NoteCloudVC: NSViewController {
    // Declare an array of Note objects for populating the table view
    var notesArray: [Note] = []
    // IBOutlets
    @IBOutlet weak var tableView: NSTableView!
    // ViewDidLoad
    override func viewDidLoad() {
        super.viewDidLoad()
        // set the tableViews delegate and dataSource to self
        tableView.delegate = self
        tableView.dataSource = self
        //Establsih R/W connection to the db
        do {
            let path = NSSearchPathForDirectoriesInDomains(
                .applicationSupportDirectory, .userDomainMask, true
            ).first! + "/" + Bundle.main.bundleIdentifier!
            // create parent directory iff it doesn’t exist
            try FileManager.default.createDirectory(
                atPath: path,
                withIntermediateDirectories: true,
                attributes: nil
            )
            let db = try Connection("\(path)/db.sqlite3")
            //Define the Notes Table and its Columns
            let notes = Table("Notes")
            let id = Expression<Int64>("ID")
            let title = Expression<String>("Title")
            let body = Expression<String>("Body")  
            /*
             Query the data from NotesAppDB.sqlite3 into an array of Note objs
             Then use that array to populate the NSTableView
             */
            for note in try db.prepare(notes) {
                let noteToAdd = Note(Int(note[id]), note[title], note[body])
                notesArray.append(noteToAdd)
            }
        } catch {
            print(error)
        }
    }
    // viewWillAppear
    override func viewWillAppear() {
        super.viewWillAppear()
        tableView.reloadData()
    }
}

// NSTableViewDataSource Extension of the NoteCloudVC
extension NoteCloudVC: NSTableViewDataSource {
    // Number of rows ~ returns notesArray.count
    func numberOfRows(in tableView: NSTableView) -> Int {
        return notesArray.count
    }
}

// NSTableViewDelegate extension of the NoteCloudVC
extension NoteCloudVC: NSTableViewDelegate {
    // Configures each cell to display the title of its corresponding note
    func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
        //configure the cell
        if tableColumn?.identifier == NSUserInterfaceItemIdentifier(rawValue: "NotesColumn") {
            
            let cellIdentifier = NSUserInterfaceItemIdentifier(rawValue: "NotesCell")
            guard let noteCell = tableView.makeView(withIdentifier: cellIdentifier, owner: self) as? NotesCell else { return nil }
            
            let note = notesArray[row]
            noteCell.noteTitle.stringValue = note.title
            
            return noteCell
        }
        return nil
    }
}

// NotesCell class
class NotesCell: NSTableCellView {
    // IBOutlet for the title
    @IBOutlet weak var noteTitle: NSTextField!
}

I'm pretty familiar with UIKit so I thought the learning curve of AppKit would be a little better than SwiftUI, so if anyone could provide some guidance about where I've gone wrong that would be very much appreciated. Also if it will be a better use of my time to turn towards SwiftUI please lmk.


Here's the values while debugging:All the values are there in the table

Note object at notesArray[0]

It's reading the values from the table correctly, so I've at least I know the problem lies somewhere in the tableView functions.

The most confusing part is the fact that the header doesn't even show up. This is all I see when I run it:

Empty window upon run


Here are some images of my storyboard as well: Outlets of the tableView

Inspector of the column

Inspector of the cell

This is for an assignment for my software modeling and design class where my professor literally doesn't teach anything. So I'm very thankful for everyone who helps with this issue because y'all are basically my "professors" for this class. When I move the tableView to the center of the view controller in the story board I can see a little dash for the far right edge of the column but that's it, and I can't progress any further without this tableView because the whole app is dependant upon it.



Solution 1:[1]

So, it turns out that the code itself wasn't actually the problem. I had always used basic swift files when writing stuff for iOS so it never occured to me that I'd need to import Cocoa to use AppKit but that's where the problem lied all along. Using this code inside the auto-generated ViewController class that had Cocoa imported did the trick. Also I got rid of the extensions and just did all the Delegate/ DataSource func's inside the viewController class.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 JonGrimes20