WWDC23 SALE: Save 50% on all my Swift books and bundles! >>

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

1      

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

2      

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.

1      

Save 50% in my WWDC23 sale.

SAVE 50% To celebrate WWDC23, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

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.