Group Group Group Group Group Group Group Group Group

Can't create an row object when using rowViewForRow

I want to use the rowViewForRow for checking what color the background must be.
I have a stuct array with multiple data and it is an ranking.
When there is an first place the background has to become blue.
When not the background has to be white.
But when i run rowViewForRow the row isn’t created and nil.
In debug mode i can see that every record in the stuct array is passed and looped.

I’ve also tried to use the tableview.makeView but with the same result.
There is an nil on the new row.

func tableView(_ tableView: NSTableView, rowViewForRow row: Int) -> NSTableRowView? {
    if tableView == tableview {
        guard let _row = tableView.rowView(atRow: row, makeIfNecessary: false) else {
            print("Rij: \(row) tableview: \(tableView)")
            
            return nil
        }
        
        if self.pouleStandenStruct[row].plaatsing == 1 {
            _row.backgroundColor = .blue
            
            return _row
        } else {
            _row.backgroundColor = .gray
            
            return _row
        }
    }
    
    return nil
}

The output in my debug window is:
Rij: 0 bij tableview: <NSTableView: 0x100655e80>
Rij: 1 bij tableview: <NSTableView: 0x100655e80>
Rij: 2 bij tableview: <NSTableView: 0x100655e80>
Rij: 3 bij tableview: <NSTableView: 0x100655e80>
Rij: 4 bij tableview: <NSTableView: 0x100655e80>
Rij: 5 bij tableview: <NSTableView: 0x100655e80>

At this time i fixed it with:
func tableView(_ tableView: NSTableView, didAdd rowView: NSTableRowView, forRow row: Int) {

This does what i want it to do.

@erich_snijder Really glad you fixed it! Cheers! :]

1 Like

Thanks.

But still don’t understand what i can do with rowViewForRow.
Try some things later and see what it’s made for.

Hi @erich_snijder,
glad you got it fixed, mac osx APIs are a bit too complex and some of them sound repetative or redundant. iOS on the other had is much more concise and better.

cheers,

This topic was automatically closed after 166 days. New replies are no longer allowed.