3

I have an app that sections and it lets user add items to a certain section. For example, the use can add "wash car" to the Monday section. I am having trouble getting the delete function to work, so that the user can remove the item if they by chance added it or have completed the item. So far what I have give me either a "could not cast value of type" error or "index out of range" error. I'm looking to see if this is the correct way of removing items or if there is different way.

 func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath)   {
    if editingStyle == UITableViewCellEditingStyle.delete   {
        //list.remove(at: indexPath.row)


        list.remove(at: [[indexPath.row]] as Any as! Int)
        UserDefaults.standard.set(list, forKey: "list")

        tableView.reloadData()
    }
}

1 Answer 1

6

I'm not sure how your 2D array is setup. I assume list is an array of an array of "tasks" where each array in list represents a day of the week. I also assume that your table view is setup in sections, like one section per day as you describe a "Monday section" where each of the "tasks" are a cell under that section. In this case, you can remove the task from the specific day as follows

list[indexPath.section].remove(at: indexPath.row)

Also, since this is in the table view commit method, I'm pretty sure you don't need tableView.reloadData().

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

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.