O.Naeem
O.Naeem

Reputation: 45

How to delete a row in NSTableView (Swift)

I have been able to add strings/data to a table from secondViewController to firstViewController but now I want users to be able to delete rows as well. How will I be able to delete the row?

Update: So here is some more detail: This is the code that I am using to create table in 1stVC.

func numberOfRows(in tableView: NSTableView) -> Int {
    return arrayData.count
}

func tableView(_ tableView: NSTableView, objectValueFor tableColumn: NSTableColumn?, row: Int) -> Any? {
    return arrayData[row]
}

I have set an array as well that is getting the data from 2ndVC through a delegate. The values from 2ndVC are stored in a variable called "data" in the 1stVC and then added to an array like so:

arrayData.append(data!)

Now I want to put a delete button, that can remove the rows/inserted data.

Thanks!

Upvotes: 1

Views: 2406

Answers (1)

vadian
vadian

Reputation: 285059

The usual way is to remove the object from the data source array and then call removeRowsAtIndexPaths:withAnimation: on the table view

if let index = arrayData.index(of: object) {
    arrayData.remove(at: index)
    let indexSet = IndexSet(integer:index)
    tableView.removeRows(at:indexSet, withAnimation:.effectFade)
}

Upvotes: 5

Related Questions