bCM bCM - 3 months ago 17
Swift Question

Deselect Tablerow remove indexrow

When I select multiple rows they should add in the array with a maximum size of 2.

For example I select row 1 and 2 with names "A" and "B". If I deselect "A" I want the array to remove the value.

I select
["A", "B"]

I decide to deselect "A" then it should remove A from the array ["B"]

var rows: [String] = []

func tableView(tableView: UITableView, willSelectRowAtIndexPath indexPath: NSIndexPath) -> NSIndexPath? {
rows.append(values[indexPath.row])
}

func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
rows.remove(values[indexPath.row])
}

extension Array where Element: Equatable {
func remove(e: Element) {
// Iam stuck here
}
}

Answer

Here Is What You Want:

extension Array where Element: Equatable {

    mutating func remove(e: Element) {
        if let i = self.indexOf(e) {
            self.removeAtIndex(i)
        }
    }
}
Comments