0

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.

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
}

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

  • The output in my debug window is: Rij: 0 bij tableview: Rij: 1 bij tableview: Rij: 2 bij tableview: Rij: 3 bij tableview: Rij: 4 bij tableview: Rij: 5 bij tableview: – Erich Snijder Jun 23 '19 at 12:22
  • Did you try `makeIfNecessary: true`? – Willeke Jun 24 '19 at 09:11
  • I have try'd it but nothing changed. But when adding: func tableView(_ tableView: NSTableView, didAdd rowView: NSTableRowView, forRow row: Int) { It all works good. Don't understand why the rowViewForRow didn't work. – Erich Snijder Jun 24 '19 at 14:45
  • Are you sure the row index you passed in was valid when makeIfNecessary was true? – Giles Jul 17 '19 at 08:34
  • The row index is done correct. As you can see in the output after the text 'Rij:'. The rows are looped correct. – Erich Snijder Jul 21 '19 at 10:56

0 Answers0