0
votes

I am at a dead-end with my code. I have a cell-view NSTableView where I import a csv file. I would like to use the tableview as a Spreadsheet. If I hit the return key after editing the specific cell, it should move to the next row, highlight it and make it editable.

I have found code written in objective c but none works for me.

How do I do that? Hope somebody will be able to assist.

Thanks in advance.

Code I have:

func controlTextDidEndEditing(_ notification: Notification) {
        let editedColumn: Int = tableMain.editedColumn
        let editedRow: Int = tableMain.editedRow
        let lastRow: Int = tableMain.numberOfRows
        let lastCol: Int = tableMain.numberOfColumns
        var userInfo = notification.userInfo
        let textMovement = Int(truncating: userInfo?["NSTextMovement"] as? NSNumber ?? 0)

        //super.textDidEndEditing(notification)

        if textMovement == NSTabTextMovement {
            if editedColumn != lastCol - 1 {
                tableMain.selectRowIndexes(NSIndexSet(index: editedRow) as IndexSet, byExtendingSelection: false)
                tableMain.editColumn(editedColumn + 1, row: editedRow, with: nil, select: true)
            } else {
                if editedRow != lastRow - 1 {
                    tableMain.editColumn(0, row: editedRow + 1, with: nil, select: true)
                } else {
                    tableMain.editColumn(0, row: 0, with: nil, select: true) // Go to the first cell
                }
            }
        } else if textMovement == NSReturnTextMovement {
            if editedRow != lastRow - 1 {
                tableMain.selectRowIndexes(NSIndexSet(index: editedRow + 1) as IndexSet, byExtendingSelection: false)
                tableMain.editColumn(editedColumn, row: editedRow + 1, with: nil, select: true)
            } else {
                if editedColumn != lastCol - 1 {
                    tableMain.editColumn(editedColumn + 1, row: 0, with: nil, select: true)
                } else {
                    tableMain.editColumn(0, row: 0, with: nil, select: true) //Go to the first cell
                }
            }
        }
}
1
@Willeke thanks, but it still does not solve the issue. I am using swift. Have converted the code from objective c to swift, but still does not work.LG Coetzee
Post your code please.Willeke
@Willeke code posted in top question. Thank youLG Coetzee
You are strongly discouraged from using cell-based table views nowadays.vadian

1 Answers

0
votes

I managed to solve my issue, probably not the best method, but works 100% for me.

I changed my tableView to View-Based. I then added an action for the TextFields. After the action is performed, I entered the following code:

 tableWages.selectRowIndexes(NSIndexSet(index: tableWages.selectedRow + 1) as IndexSet, byExtendingSelection: false)
 tableWages.editColumn(colNr, row: tableWages.selectedRow, with: nil, select: true)

So after editing the value and the Return Key is pressed, it moves to the next row and stays in the same column, making the next value editable.