0

I have a NSTableView with view based cells, TaskCell and NonTaskCell, and i create them like this:

func tableView (tableView: NSTableView,
    viewForTableColumn tableColumn: NSTableColumn?, row: Int) -> NSView? {

    let cellRow = row
    var theData = data[row]
    var cell: TaskCellProtocol? = nil
    switch Int(theData.taskType!.intValue) {
        case TaskType.Issue.rawValue:
            cell = self.tableView?.makeViewWithIdentifier(kTaskCellIdentifier, owner: self) as? TaskCell
        default:
            cell = self.tableView?.makeViewWithIdentifier(kNonTaskCellIdentifier, owner: self) as? NonTaskCell
    }
    assert(cell != nil, "Cell can't be nil, check the identifier")

    // Add data to the cell
    TaskCellPresenter(cell: cell!).presentData(theData, andPreviousData: row < data.count-1 ? data[row+1] : nil)

    cell?.didAddCell = { (cell: NSTableRowView) in
        RCLog(cell)
        let row = tableView.rowForView(cell as! NSTableRowView)
        tableView.enumerateAvailableRowViewsUsingBlock({ (rowView, rowIndex) -> Void in
            RCLog(rowView)
            if rowView == cell as! NSTableRowView {
                RCLog("found it")
                self.didAddRow?(row: cellRow)
            }
        })
    }
    RCLog(cell)
    return cell as? NSView
}

When i create the cells they have the row -1 as this logs show:

TasksScrollView.swift:114: Optional(<Jirassic.TaskCell: 0x100e1eb60> - row: -1)
TasksScrollView.swift:114: Optional(<Jirassic.TaskCell: 0x100e269f0> - row: -1)
TasksScrollView.swift:114: Optional(<Jirassic.TaskCell: 0x100b120e0> - row: -1)
TasksScrollView.swift:114: Optional(<Jirassic.NonTaskCell: 0x100b181d0> - row: -1)

When i click a button inside the cell i call didAddCell with the cell itself as a parameter. RCLogCell(cell) is

TasksScrollView.swift:101: <Jirassic.TaskCell: 0x100e269f0> - row: -1

The logs in the enumerator are:

TasksScrollView.swift:107: <NSTableRowView: 0x6000001a24c0> - row: 0
TasksScrollView.swift:107: <NSTableRowView: 0x6000001a1b20> - row: 1
TasksScrollView.swift:107: <NSTableRowView: 0x6180001a0540> - row: 2
TasksScrollView.swift:107: <NSTableRowView: 0x6180001a07e0> - row: 3

From the apple documentation i read that if the cell is not NSTableRowView or a subclass, rowForView will report -1. However my TaskCell it is a NSTableRowView and i don't get what's happening. It seems that created another rows internally. Any ideas? You can also follow the code on git https://github.com/ralcr/Jirassic/blob/master/Jirassic/TasksScrollView.swift

Cristi Băluță
  • 1,295
  • 15
  • 27

1 Answers1

0

Update: I found that my TaskCell is a subview of the cell: rowView.subviews.first! I don't know why is like this but seems that i can find the correct index now with the iterator

Cristi Băluță
  • 1,295
  • 15
  • 27