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
How to Get Section of UItableview from Inside a Child UIcollectionview
Macos Remote Push Notifications Not Showing Alert Banner
Indent Second Line of UIlabel (Swift)
Place Multiple Scn Objects in Touchesbegan Method
Create Skspritenode with an Asset Programmatically
Pass Data from Tableviewcontroller to Another Tableviewcontroller in Swift
It Is Posible to Load Customise HTML View into Webview in Swift
How to Set Window Level in Swift
Could Not Find an Overload for '-' That Accepts The Supplied Arguments
Return Object for a Method Inside Completion Block
How to Disabled Some Default Functionality in Scene View When Allowscameracontrol = True
Osx/Swift: Call a Function When Internet Connection Is Established/Restored
Sending Array Data from One View Controller to Another
Disable Bringing App Window to Front. After Closing Another Window
Swiching Between 2 Diferent Nsviewcontrollers with Data
Skphysicscontact Not Detecting Categorybitmask Collision
Need Clarification on Anyobject in Swift
Updating Existing Constraints Does Not Work, Wrong Use of .Active Property