3

I am working on expandable tableView where I need to add or remove the objects based on didSelectRowAtIndexpath. I was able to add the selected item into an array but the problem is when I am trying to remove the selection from array.

This is the error I am getting:

cannot invoke index with an argument list of type (of:any)

Here is my code:

func expandableTableView(_ expandableTableView: LUExpandableTableView, didSelectRowAt indexPath: IndexPath) {
    let selectedService = arrData[indexPath.section].Children?[indexPath.row].EnglishName ?? ""
    let inPrice = arrData[indexPath.section].Children?[indexPath.row].InPrice ?? 0

    print("service and price : \(selectedService) \(inPrice)")

    let selectedItem = (" \(selectedService) \(inPrice)")
    let cell: UITableViewCell? = expandableTableView.cellForRow(at: indexPath)

    if cell?.accessoryType == UITableViewCellAccessoryType.none
    {
        cell?.accessoryType = UITableViewCellAccessoryType.checkmark
        selectionArr.append(selectedItem)
    }
    else
    {
        cell?.accessoryType = UITableViewCellAccessoryType.none
        selectionArr.remove(at: selectionArr.index(of: selectedItem)!)
    }

    print(selectionArr)
}
3
  • What type is selectionArr? Commented Aug 6, 2017 at 10:39
  • var selectionArr = [Any]() Commented Aug 7, 2017 at 16:45
  • [Any] causes the issue. Declare selectionArr to something more specific. It seems to be an array of string, so [String]. Commented Aug 7, 2017 at 17:01

3 Answers 3

2

@AnhPham

Hey, Tried modifying your code and it worked well please go through following for adding item in array and removing from the same on tableview's didselect and diddeselect methods respectively.

 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    let categoryTableCell = self.categoriesTableview.cellForRow(at: indexPath) as! CategoriesTableCell

    self.tempCategories.append(self.categories[indexPath.row])

    print("tempArray select:\(self.tempCategories)")

}

func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {

    let categoryTableCell = self.categoriesTableview.cellForRow(at: indexPath) as! CategoriesTableCell

    self.tempCategories.remove(at: self.tempCategories.index(of:self.categories[indexPath.row])!)

    print("tempArray deselect:\(self.tempCategories)")

}
Sign up to request clarification or add additional context in comments.

Comments

0

selectionArr.remove(at: selectionArr.index(of: selectedItem)!)

selectedItem is not an Int. You should receive an error:

"Cannot convert value of type 'String' to expected argument type 'Int'"

Maybe you are just searching this one:

selectionArr.remove(at: indexPath.row)

3 Comments

its working fine but at a certain point of time the index is getting out of range and resulting error " fatal error: Index out of range",
we are adding objects into the array not based on indexpath.row, when we try to delete the object based on indexpath.row its going into exception as "index out of range"
of course you have to make sure that cellsInSection returns the amount of your array.count otherwise of course the amount of items in your array related to items in the view does not match another array ... the answer with the remove is fine. you should use some prints to get it ... or provide more code with the related functions
0

Use indexPath.row instead of selectedItem selectionArr.remove(at: selectionArr.index(of: indexPath.row)!)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.