Making table view editable - ability to insert/delete items

Just being able to edit existing items was not enough for me. The user should also be able to insert items between other items. The user should be able to delete items.

Look ahead to p326 - 328 (print version) to review (or learn) how menu items relate to the First Responder.

Add the following action methods to MainWindowController.swift:

[code] @IBAction func deleteRowItem(sender:AnyObject?) {
let row = tableView.selectedRow
if row > -1 {
println(“deleteRowItem(_:slight_smile: will delete row (row)”)
}
}

@IBAction func insertItemBeforeSelectedRow(sender:AnyObject?) {
    let row = tableView.selectedRow
    if row > -1 {
        println("insertItemBeforeSelectedRow(_:) will insert an item before row \(row)")
    }
}

[/code]

In MainMenu.xib, add a List menu to the menubar with menu items “Delete Item” and “Insert Item”.

Control drag from the item “Delete Item” to the First Responder placeholder in the outline. A list of action methods within the responder chain will pop up; locate the action method deleteRowItem: and connect “Delete Item” to deleteRowItem: . Do the same to connect “Insert Item” to insertItemBeforeSelectedRow: .

Run the application and enter some items. With no items selected, choose either “Delete Item” or “Insert Item”; nothing will happen. Now select a row and test the new menu items again. You will see the appropriate message in the console.

Great challenge! Thanks for posting.