How to create a custom NSTableCellView from a NIB? - swift

I'm new to Swift and I am struggling with NSTableView! I'm trying to create a custom NSTableCellView from a NIB.
I want to load the cell from a NIB because:
it will be reused in multiple columns and multiple table-views
it will be visually and functionally complex (relatively)
it is likely to evolve during development
I'm able to load the cell in my table view but I'm getting a "Failed to connect outlet from... missing setter or instance variable" error in the debug area when I try to populate the view with data. The custom cell is visible in the tableview but doesn't seem to be instantiated.
I've searched for a solution online for hours! Help what am I missing?
This is my TableViewController...
protocol TableViewDelegate {
func itemWithIndexWasSelected(value: Int)
}
class TableViewController: NSViewController {
#IBOutlet weak var tableView: NSTableView!
let tableViewData =
[ [ "Column1": "John", "Column2": "Smith", "Hobby": "Birds"],
[ "Column1": "Jane", "Column2": "Doe", "Hobby": "Fish"],
[ "Column1": "Hal", "Column2": "Bernard", "Hobby": "Trees"],
[ "Column1": "Harry", "Column2": "Bell", "Hobby": "Rocks"] ]
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
let customCellNib = NSNib.init(nibNamed: "CustomTableCellView", bundle: nil)
tableView.register(customCellNib, forIdentifier: NSUserInterfaceItemIdentifier("CustomCellView"))
tableView.reloadData()
}
}
extension TableViewController: NSTableViewDataSource, NSTableViewDelegate {
func numberOfRows(in tableView: NSTableView) -> Int {
return tableViewData.count
}
func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
if tableColumn?.identifier.rawValue == "CustomCell" {
let result: CustomTableCellView = tableView.makeView(withIdentifier: NSUserInterfaceItemIdentifier("CustomCellView"), owner: self) as! CustomTableCellView
result.hobbyLabel?.stringValue = tableViewData[row]["Hobby"]!
result.hobbyButton?.title = "TESTTITLE"
return result
}
else {
let result = tableView.makeView(withIdentifier:(tableColumn?.identifier)!, owner: self) as! NSTableCellView
result.textField?.stringValue = tableViewData[row][(tableColumn?.identifier.rawValue)!]!
return result
}
}
}
I have a CustomTableCellView with a XIB that has the same name...
class CustomTableCellView: NSTableCellView {
#IBOutlet weak var hobbyButton: NSButton!
#IBOutlet weak var hobbyLabel: NSTextField!
}
I have a test project that I can send or upload... help will be hugely appreciated!
This is what I am seeing:

When editing a xib, the File's Owner proxy object represents the object which is passed as owner to makeView(withIdentifier:owner:) at runtime. In this case the owner object is the view controller. You can set the class of the File's Owner in the xib to TableViewController and connect actions. You can't set the class of the File's Owner to CustomTableCellView and connect outlets. Instead connect the outlets of the CustomTableCellView view object.

Related

Table Content disappears on Scroll in TableView with Custom Cell using Subview - Swift

I have a ViewController which uses multiple Subviews (HomeViewController, etc.) which can be selected via a Custom Tab Bar at the bottom of my app. Inside the HomeViewController there is a UIView containing a UITableView containing a Prototype Custom Cell with name and image.
import UIKit
class HomeViewController: UIViewController {
#IBOutlet weak var friendView: UITableView!
let friends = ["batman", "harsh", "ava", "sasha", "fatima", "alfred"]
override func viewDidLoad() {
super.viewDidLoad()
friendView.delegate = self
friendView.dataSource = self
friendView.allowsSelection = false
}
}
extension HomeViewController: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 120
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return friends.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = friendView.dequeueReusableCell(withIdentifier: "customCell") as! CustomCell
let friend = friends[indexPath.row]
cell.avatarImg.image = UIImage(named: friend)
cell.nameLbl.text = friend
return cell
}
}
Custom cell:
import UIKit
class CustomCell: UITableViewCell {
#IBOutlet weak var friendView: UIView!
#IBOutlet weak var nameLbl: UILabel!
#IBOutlet weak var avatarImg: UIImageView!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
When I start the app, everything looks just fine. However, when I start scrolling inside the table, all data suddenly disappears. All relations between storyboard and code should be just fine. I think it might have got something to do with my need of using a Subview.
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var tabBarView: UIView!
#IBOutlet weak var contentView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
Design.makeCornersRound(view: tabBarView, radius: 10.0)
Timer.scheduledTimer(withTimeInterval: 0.1, repeats: false) { (timer) in
self.switchToHomeViewController()
}
}
#IBAction func onClickTabBar(_ sender: UIButton) {
let tag = sender.tag
if tag == 1 {
switchToIncomingsViewController()
}
else if tag == 2 {
switchToSpendingsViewController()
}
else if tag == 3 {
switchToHomeViewController()
}
else if tag == 4 {
switchToSavingsViewController()
}
else if tag == 5 {
switchToSettingsViewController()
}
}
func switchToHomeViewController() {
guard let Home = self.storyboard?.instantiateViewController(withIdentifier: "HomeViewController") as? HomeViewController else { return }
contentView.addSubview(Home.view)
Home.didMove(toParent: self)
}
...
}
Reference to the tutorial I have been trying to implement: https://www.youtube.com/watch?v=ON3Z0PXSoVk
In this function:
func switchToHomeViewController() {
// 1
guard let Home = self.storyboard?.instantiateViewController(withIdentifier: "HomeViewController") as? HomeViewController else { return }
// 2
contentView.addSubview(Home.view)
// 3
Home.didMove(toParent: self)
// 4
}
At 1 you create an instance of HomeViewController
at 2 you add its view to cotentView
at 3 you call didMove() ... but that doesn't do anything because you haven't added the controller to your hierarchy
at 4 your Home instance goes away, so the code in that controller no longer exists
You need to add the controller as a child controller.
As a side note, use lowerCase for variable names:
func switchToHomeViewController() {
// create an instance of HomeViewController
guard let homeVC = self.storyboard?.instantiateViewController(withIdentifier: "HomeViewController") as? HomeViewController else { return }
// add it as a child view controller
self.addChild(homeVC)
// add its view
contentView.addSubview(homeVC.view)
// here you should either set the view's frame or add constraints
// such as:
homeVC.view.frame = contentView.bounds
// inform the controller that it moved to a parent controller
homeVC.didMove(toParent: self)
}

How to add additional textfields by clicking button in table view

I am trying to add an option to add additional student fields inside table so that user can add more than one student name.
But I am confused how to do it using table view.
I am not interested in hiding view with specific number of fields.
class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
struct listItems{
var title : String
var isExpandable:Bool
var maxFields :Int
init(title:String,isExp:Bool,mxF:Int) {
self.title = title
self.isExpandable = isExp
self.maxFields = mxF
}
}
#IBOutlet weak var tblListTable: UITableView!
let data : [listItems] = [listItems(title: "Name", isExp: false, mxF: 1), listItems(title: "Student Name", isExp: true, mxF: 20), listItems(title: "Email", isExp: false, mxF: 1)]
override func viewDidLoad() {
super.viewDidLoad()
tblListTable.delegate = self
tblListTable.dataSource = self
self.tblListTable.reloadData()
print("isLoaded")
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
print("cellForRow")
let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! ListCell
cell.lblName.text = data[indexPath.row].title
if data[indexPath.row].isExpandable == true {
cell.btnAddField.isHidden = false
print("ishidden")
}
else {
cell.btnAddField.isHidden = true
}
return cell
}
}
List Cell Class
import UIKit
protocol AddFieldDelegate : class {
func addField( _ tag : Int)
}
class ListCell: UITableViewCell {
#IBOutlet weak var btnAddField: UIButton!
#IBOutlet weak var lblName: UILabel!
#IBOutlet weak var txtField: UITextField!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
func addField( _ tag : Int){
}
}
You are on the right track creating the AddFieldDelegate. However, rather than implementing the method inside the ListCell class you need to implement it in the ViewController.
First, change the view controller class definition line to:
class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource, AddFieldDelegate {
This will allow you to call the delegate method from the view controller. Next, when you are creating your table view cells add the line:
cell.delegate = self
After that, move the method definition of the method addField to the view controller.
So inside of your view controller add:
func addField(titleOfTextFieldToAdd: String, numberAssociatedWithTextFieldToAdd: Int) {
data.append(listItems(title: titleOfTextFieldToAdd, isExp: false, mxF: numberAssociatedWithTextFieldToAdd))
self.tableView.reloadData()
}
I used an example definition of the addField method but you can change it to anything that you would like, just make sure that you change the data array and reload the table view data.
Lastly, we must define the delegate in the ListCell class. So add this line to the ListCell class:
weak var delegate: MyCustomCellDelegate?
You can then add the text field by running the following anywhere in your ListCell class:
delegate?.addField(titleOfTextFieldToAdd: "a name", numberAssociatedWithTextFieldToAdd: 50)
For more information on delegation, look at the answer to this question.
You have to append another item in your data array on button click and reload the tableview.

Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value after I try to reloadData()

I am creating an app that scans barcodes and adds the barcode value (and name and date but working on that later). After I scan the barcode, I call the add method which should add it to my tableview but I am hitting an error at my reloadData().
import UIKit
class FirstViewController: UIViewController {
#IBOutlet weak var tableView: UITableView!
var data: [Ticket] = []
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
data = createArray()
}
func createArray() -> [Ticket] {
let video1 = Ticket(number: "123456789", name: "First Name - Last Name", date: "May 18th, 2019, 7 am")
let video2 = Ticket(number: "123456789", name: "First Name - Last Name", date: "May 18th, 2019, 7 am")
return [video1, video2]
}
}
extension FirstViewController: UITableViewDataSource, UITableViewDelegate {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let ticket = data[indexPath.row]
let cell = tableView.dequeueReusableCell(withIdentifier: "TicketCell") as! TicketCell
cell.setTicket(ticket: ticket)
return cell
}
func add (_ code: String)
{
let tic = Ticket(number: code, name: "First Name - LastName", date: "May 18th, 2019, 10 am")
print(tic.number1)
print(tic.name1)
print(tic.date1)
data.append(tic)
self.tableView.reloadData() // Error Occurs Here
}
}
This is where I'm calling the add method from a different controller. The String "code" is the barcode's value:
func found(code: String) {
FirstViewController().add(code)
viewDidLoad()
}
Each time you add a barcode using FirstViewController(), you are creating a new instance of FirstViewController, you can use delegation to update the original instance.
First create a protocol with your add method
protocol BarcodeScanDelegate: class {
func add(_ code: String)
}
Extend FirstViewController to inherit BarcodeScanDelegate and move the add method
extension FirstViewController: BarcodeScanDelegate {
func add (_ code: String) {
let tic = Ticket(number: code, name: "First Name - LastName", date: "May 18th, 2019, 10 am")
self.data.append(tic)
self.tableView.reloadData()
}
}
Within the 2nd (barcode scan) view controller create a variable that keeps a reference to the above delegate
class BarcodeScanViewController: UIViewController {
weak var delegate: BarcodeScanDelegate?
func addScannedBarcode() {
self.delegate?.add("BARCODE_HERE")
}
}
Now when you launch 2nd (barcode scan) view controller, assign its variable delegate to self
class FirstViewController: UIViewController {
#IBOutlet var tableView: UITableView!
...
#objc func launchBarcodeScanner() {
let viewController = BarcodeViewController()
// or use following if you are using storyboard
// let viewController = self.storyboard!.instantiateViewController(withIdentifier: "BarcodeVC") as! BarcodeViewController
viewController.delegate = self // <<<
self.present(viewController, animated: true)
}
}
You have the tableView in FirstViewController class in storyboard.
But in this method you are not appending a ticket to the existing array. You are creating new instance for FirstViewController. The tableview is in storyboard and you are creating this FirstViewController instance programmatically. So the IBOutlet will be nil. So it is crashing.
func found(code: String) {
FirstViewController().add(code)//new FirstViewController instance
viewDidLoad()
}
Instead of adding the code from another view controller, pass the code to the FirstViewController using custom delegate and reload the tableview.

Set the outlet of the item in TableCellView within the .xib file to the custom NSTableCellView subclass

I want to fill my NSTableView with content. Per table-cell-row are 3 items (2 NSTextFields and 1 NSImageView). For that I created a custom NSTableCellView where I want to set the #IBOutlets of the 3 Items, to set there the value for them. But when I try to set the referencing outlets, the only option is to create an action.
When I try to write #IBOutlet weak var personName: NSTextfield and then set the references, I can't because "xcode cannot locate the class in the current workspace"
When I create the NSTableViewinside a main.storyboard, I'm able to set the outlet references. So what is the different behavior between .storyboard and .xib?
When I try to connect the #IBOutlet with the Item "Person Name"
My NSViewController (owner of the .xib)
class ViewController: NSViewController, NSTableViewDataSource, NSTableViewDelegate {
#IBOutlet weak var tableView: NSTableView! //ref to tableView in xib
var persons:[Person] = [] //content to fill tableview
override func viewDidLoad() {
super.viewDidLoad()
persons.append(Person(name: "John", age: 23, piRef: "/Users/xy/Desktop/profilePic.png"))
persons.append(Person(name: "Marie", age: 26, piRef: "/Users/xy/Desktop/profilePic.png"))
tableView.delegate = self
tableView.dataSource = self
}
func numberOfRows(in tableView: NSTableView) -> Int {
return persons.count
}
func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
let tableCellView:personTableCell = tableView.makeView(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "defaultRow"), owner: self) as! personTableCell
//NSTableColumn in xib has id "defaulRow"
if let person:Person = persons[row] {
tableCellView.setPerson(person: person) //call method inside NSTableCellView-subclass to set item values
}
return tableCellView
}
}
The custom NSTableCellView subclass ("personTableCell")
class personTableCell: NSTableCellView {
var person:Person! = nil
//here should be:
//#IBOutlet weak var personName: NSTextField!
//#IBOutlet weak var personAge: NSTextField!
//#IBOutlet weak var personImg: NSImageView!
func setPerson(person: Person) {
self.person = person
self.personName = person.name
self.personAge = person.age
self.personImg = NSImage(byReferencingFile: person.profileImgRef)
}
}
I want to be able to add the item outlet references to my NSTableCellView-subclass.
It appears to me you're making this harder than it needs to be. makeView is giving you a reference to the cell. Therefore you can access its members directly. No need for outlets (which is why Xcode won't make them for you.)
I can't read your screenshots well enough to tell how the textfields are defined (old eyes), so I can only give you a generic example from a working demo of a custom cell class:
class DIYTableViewDelegate: NSObject, NSTableViewDelegate {
var count = 0 // counts the number of views actually created
func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
let id = tableColumn!.identifier
var view = tableView.makeView(withIdentifier: id, owner: nil) as? CustomTableCellView
if view == nil {
view = createCell(id)
count += 1
}
view!.textField!.stringValue = "\(id.rawValue) \(row) \(view!.count) \(count)"
view!.count += 1
return view
}
}
Also, it's customary in Swift to capitalize the first letter of types (classes, structures, enums, protocols) and lowercase methods & properties. Doesn't affect how your code compiles, but it helps other Swifties read it.
Here's another example that may help:
func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
guard let vw = tableView.makeView(withIdentifier: tableColumn!.identifier, owner: self) as? CustomTableCellView else { return nil }
vw.textField?.stringValue = String(pictures[row].dropLast(4))
vw.imageView?.image = NSImage(named: pictures[row])
return vw
}

NSTableView Delegate methods won't get called

I'm currently trying to parse the reddit headlines from a specific subreddit and display these in an NSTableView. The thing is, the numberOfRows function gets called and returns the correct integer but the tableView delegate function never gets called.
As far as I can see everything is wired up correctly in the code.
ViewController:
#IBOutlet weak var tableView: NSTableView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
Downloader.load(url: URL(string: "https://www.reddit.com/r/" + "gaming" + ".json")!){
(result) in
let tvc = TableViewController(data: result)
self.tableView.delegate = tvc
self.tableView.dataSource = tvc
self.tableView.reloadData()
}
}
TableViewController:
class TableViewController: NSObject{
var json: JSON!
init(data: JSON) {
super.init()
self.json = data
}
}
extension TableViewController : NSTableViewDataSource {
func numberOfRows(in tableView: NSTableView) -> Int {
return JSONFormatController.getTitlesFrom(json: json).count
}
}
extension TableViewController : NSTableViewDelegate {
func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
var titles = JSONFormatController.getTitlesFrom(json: json)
if let cell = tableView.make(withIdentifier: "entry", owner: nil) as? NSTableCellView {
cell.textField?.stringValue = titles[row]
return cell
} else {
return nil
}
}
}
The result variable and getTitlesFrom method do work, I checked these.
I think your issue is that your TableViewController object is getting deallocated because you are not keeping a reference to it. Try this:
#IBOutlet weak var tableView: NSTableView!
var tvc : TableViewController!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
Downloader.load(url: URL(string: "https://www.reddit.com/r/" + "gaming" + ".json")!){
(result) in
self.tvc = TableViewController(data: result)
self.tableView.delegate = self.tvc
self.tableView.dataSource = self.tvc
self.tableView.reloadData()
}
}
Explanation: tvc is a local variable of the download block which is getting deallocated after it has executed. Presumably your assumption is that storing the tvc in delegate and/or dataSource is keeping tvc alive. But they are not, they are weak references.