How to Remove Items from an Array When Deselecting a Row in a UItableview

How to remove items from an array when deselecting a row in a UITableView

You can just check whether the newFruitList contains the item you want to add by getting index of that item in your list.

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
newFruitList.append(fruits[indexPath.row])
print("New List: \(newFruitList)")
}

func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
if let index = newFruitList.index(of: fruits[indexPath.row]) {
newFruitList.remove(at: index)
}
print("New List: \(newFruitList)")
}

Unable to remove tableview's deselected row array value in Swift

Modify the code of checkBoxSelection() method like below

if self.arrSelectedRows.contains(sender.tag){
if let selectedIndex = self.arrSelectedRows.firstIndex(of: sender.tag) {
self.arrSelectedRows.remove(at: selectedIndex)
arrSubCat.remove(at: selectedIndex)
}
}

How to remove items from an array when deselecting a row in a UITableView

You can just check whether the newFruitList contains the item you want to add by getting index of that item in your list.

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
newFruitList.append(fruits[indexPath.row])
print("New List: \(newFruitList)")
}

func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
if let index = newFruitList.index(of: fruits[indexPath.row]) {
newFruitList.remove(at: index)
}
print("New List: \(newFruitList)")
}

Remove objects from array when deselect table view

If you have an array, itemsArr, and you know the id of the item that you want to remove, you can simple use,

let filteredArray = itemsArr.filter { $0.id != idToDelete }

Removing Array element when user didDeselectRowAt?

Okay, so I think you are displaying a list of rows, and when a cell is selected, you add the user to the transferUsers array. The issue with this is that you do not store the location of the selected user.

A quick fix for this would be to have a dictionary, which combines the index path with the user.

var transferUsers: [IndexPath: UserModel] = [:]

then, when a user is selected, you do this:

transferUsers[indexPath] = usersArray[indexPath.row]

and when a user is deselected, you do this:

transferUsers.removeValue(forKey: indexPath)

selectedUsersCount could be a property:

var selectedUsersCount: Int {
return transferUsers.count
}

to get only the list of selected users, since you have a dictionary, you would do it like this:

transferUsers.values

Removing Value from Array when Deselect Cell

Why are you calling let indexPath = tableView.indexPathForSelectedRow()

The indexPath parameter of the function gives you the indexPath of the deselected row.

override func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
let deselectedCell = tableView.cellForRowAtIndexPath(indexPath!) as UITableViewCell!
selectedFoodTypes = selectedFoodTypes.filter {$0 != deselectedCell.textLabel!.text!}
println(selectedFoodTypes)
}

The above code might work. But comparing with textLabel.text of the cell is not a good idea. If datasourceArray is an array for example and set as the tableView dataSource, you can do

override func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
let deselectedCell = tableView.cellForRowAtIndexPath(indexPath!) as UITableViewCell!
selectedFoodTypes = selectedFoodTypes.filter {$0 != datasourceArray[indexPath.row]}
println(selectedFoodTypes)
}

Remove item from array in UITableview

If you have an array :

var cast = ["Vivien", "Marlon", "Kim", "Karl"]

and you want to remove "Marlon" from it, you can find the index of Marlon using the func firstIndex(of: Element) -> Int? method for an array and then remove it like so :

if let index = cast.firstIndex(of: "Marlon"){
cast.remove(at: index)
print(cast)
}

That said, on didSelectRowAt run this function for your array and you'll accomplish what you're looking to.



Related Topics



Leave a reply



Submit