Swift TableView: Saving Text from TextField

For the past few months, I’ve been learning about Swift coding. I’m relatively new to the coding field so this may seem trivial to some.

Here’s what I’m trying to do: I cannot get the data saved from regular textboxes to appear in the table I've designed on the storyboard. It is clearly saving something to the table rows but I cannot see it.

I've looked all over the web but most examples are from old versions of swift/deprecated versions of Xcode and are not applicable.

Basically, I’m designing an app for my company that allows quick and easy saving of users that call; I would be able to save their company, name, phone, userid, etc, the app saves it into the table And allows me to reference it later.

I'm using core data and I’ve attached all of the code to the appropriate storyboard fields.

any insight or errors someone could point out would be very helpful.

Here’s my code:

import Cocoa
import SwiftData
import SwiftUI


class User: NSManagedObject, Identifiable {
	let id = UUID() //compatibility
	@NSManaged public var company: String
	@NSManaged public var name: String
	@NSManaged public var phone: String
	@NSManaged public var uid: String
	@NSManaged public var cid: String
	@NSManaged public var tvid: String
	@NSManaged public var tvpwd: String
	@NSManaged public var notes: String
	
	}

class ViewController: NSViewController, NSTableViewDataSource, NSTableViewDelegate {
	
	@IBOutlet weak var companyTextField: NSTextField!
	@IBOutlet weak var nameTextField: NSTextField!
	@IBOutlet weak var phoneTextField: NSTextField!
	@IBOutlet weak var uidTextField: NSTextField!
	@IBOutlet weak var cidTextField: NSTextField!
	@IBOutlet weak var tvidTextField: NSTextField!
	@IBOutlet weak var tvpwdTextField: NSTextField!
	@IBOutlet weak var notesTextField: NSTextField!
	@IBOutlet weak var tableView: NSTableView!
	
	var users = [User]()
	
	override func viewDidLoad() {
		super.viewDidLoad()
		tableView.dataSource = self
		tableView.delegate = self
	}
	
	@IBAction func saveButtonClicked(_ sender: NSButton) {
		let user = User()
			users.append(user)
			tableView.reloadData()
		
		}
		
		// MARK: - NSTableViewDataSource
		func numberOfRows(in tableView: NSTableView) -> Int {
			return users.count
		}
		
		// MARK: - NSTableViewDelegate
		func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
			let user = users[row]
			
			guard let cell = tableView.makeView(withIdentifier: tableColumn!.identifier, owner: self) as? NSTableCellView else { return nil }
			
			switch tableColumn?.identifier.rawValue {
			case "company":
				cell.textField?.stringValue = user.company
			case "name":
				cell.textField?.stringValue = user.name
			case "phone":
				cell.textField?.stringValue = user.phone
			case "uid":
				cell.textField?.stringValue = user.uid
			case "cid":
				cell.textField?.stringValue = user.cid
			case "tvid":
				cell.textField?.stringValue = user.tvid
			case "tvpwd":
				cell.textField?.stringValue = user.tvpwd
			case "notes":
				cell.textField?.stringValue = user.notes
				
			default:
				return nil
			}
			
			return cell
			
		}
	}
![]

Replies

when pressing the save button, the strings are saving to the table. for some reason, I cannot see it.

any help would be really appreciated

Are you configuring the core data container and contexts? Also you need to insert the core data object into a table. You are importing swift data, but using core data.

  • I was trying to get data to save to temporary memory for the time being, configuring in core data was an extra step - I was just trying to get it to work properly; do you think if I get it to save to core data, it'll work?

    when you say configuring contexts, what do you mean? can you give me an example?

Add a Comment