1

I have some tableview cells with some data on them and the cells have a cross button on them (at the top right) on the click of which the cell should get deleted. This is how I'm trying to delete...

extension sellTableViewController: imageDelegate {
    func delete(cell: sellTableViewCell) {
        if let indexPath = tableview?.indexPath(for: cell) {
            //1.Delete photo from datasource
            arrProduct?.remove(at: indexPath.row)
            print(self.appDelegate.commonArrForselectedItems)

            tableview.deleteRows(at: [indexPath], with: .fade)

        }
    }
}

But when I click on the cross button I get an error message saying The number of sections contained in the table view after the update (1) must be equal to the number of sections contained in the table view before the update (2), plus or minus the number of sections inserted or deleted (0 inserted, 0 deleted).'

My tableview numberOfSections and numberOfRowsInSection is given as follows...

    func numberOfSections(in tableView: UITableView) -> Int {
        return (arrProduct?.count)!

    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        let product = arrProduct![section]

        return product.images.count
    }

Hope somebody can help...

OverD
  • 2,612
  • 2
  • 14
  • 29

2 Answers2

2

You are removing item from array at from indexPath.row but your array contains sections not rows

Just one line mistake replace

        arrProduct?.remove(at: indexPath.row)

With

        arrProduct?.remove(at: indexPath.section)

Hope it is helpful to you

EDIT

I think you are removing image from array then

arrProduct![indexPath.section].images.remove(at: indexPath.row)

Prashant Tukadiya
  • 15,838
  • 4
  • 62
  • 98
  • Thanks a lot @Jon Snow. Worked just fine. Have given an upvote also...Thanks again...:) –  Nov 23 '17 at 13:12
  • Although it's suggested again and again `begin-` and `endUpdates()` has no effect at all for a single `insert`/`delete`/`move` operation. – vadian Nov 23 '17 at 13:13
  • @bvt Glad to listen that solution worked for you , Have a great day – Prashant Tukadiya Nov 23 '17 at 13:13
  • Yup! Thanks...:) –  Nov 23 '17 at 13:14
  • Just one more concern, @Jon Snow. What you suggested did delete the cell correctly. But maybe it didn't delete that particular element from the array because of which it persists once I return to this screen again. How can I delete the element from the array in such a case..? –  Nov 23 '17 at 13:34
  • @bvt If element is not deleted from array and you delete that element from tableview app will crashed As you are getting previously , I think you are saving that data somewhere in coredata or sqlite then you also need to delete that item from there also – Prashant Tukadiya Nov 23 '17 at 13:45
  • So you are saying currently it is being deleted from the array itself...? –  Nov 23 '17 at 13:48
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/159685/discussion-between-jon-snow-and-bvt). – Prashant Tukadiya Nov 23 '17 at 13:48
0

Your code is confusing sections and rows. You are saying the number of sections is based on the number of products (arrProduct?.count) and the number of row is based on the number of images in the product for the section (arrProduct![section].images.count).

But in your delete function, you delete a product (arrProduct?.remove(at: indexPath.row)), which corresponds to a section but then you delete a row on the table.

Make sure you are clear when you are working with products (sections) and images (rows).

Gary Makin
  • 3,109
  • 1
  • 19
  • 27