UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Hacking with MacOS - Project1 - Cells not displaying "Hello World"

Forums > Books

Hi there! I'm working my way through Hacking With MacOS - Swift Addition, and while I'm still very early on, I think it's great!

Anyhow, I'm at the point where I just added the "tableView" method for my SourceViewController class, which just displays "Hello World" in every cell. That compiles and runs...but I still see the default "Table View Cell" in each cell instead of "Hello World". I double checked to make sure that the SourceViewController is the dataSource of TableView, and the outlet is created, so I'm not sure what's going on.

One other confusing thing in the book is that it says:

The second method we’re going to implement is the viewFor method of NSTableView. It gets called once every column in every row, and should return the view that should be shown in that slot.

But then goes on to describe implementing tableView, which happens to have an argument called viewFor. Is that a mistake or is there something more subtle I missing (I'm very new to the Swift language)

Thanks!

bobby

3      

Figured it out! I forgot to have NSTableViewDelegate in my class declaration!

4      

To answer this part of your question...

One other confusing thing in the book is that it says:

The second method we’re going to implement is the viewFor method of NSTableView. It gets called once every column in every row, and should return the view that should be shown in that slot.

But then goes on to describe implementing tableView, which happens to have an argument called viewFor. Is that a mistake or is there something more subtle I missing (I'm very new to the Swift language)

The method name isn't actually tableView, it's tableView(_:viewFor:row:). You'll see in the NSTableViewDelegate documentation that there are a lot of similar methods that all start with tableView, such as:

tableView(_:rowViewForRow:)
tableView(_:didAdd:forRow:)
tableView(_:didRemove:forRow:)
tableView(_:isGroupRow:)

and so on.

It's just kind of a shorthand to refer to them by the second parameter name, since that's the important part of the method that distinguishes them from the other delegate methods.

3      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.