简体   繁体   中英

Move to next NSTableview Row when return key is pressed after editing

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
                }
            }
        }
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM