18

I try to remove an item from a collection view based on a user's choice in an alert.

I have the following code:

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
    let person = people[indexPath.item]

    let questionController = UIAlertController(title: "What u wanna do?", message: nil, preferredStyle: .Alert)
    questionController.addAction(UIAlertAction(title: "Rename person", style: .Default, handler: {

        (action:UIAlertAction!) -> Void in

        let ac = UIAlertController(title: "Rename person", message: nil, preferredStyle: .Alert)
        ac.addTextFieldWithConfigurationHandler(nil)

        ac.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: nil))
        ac.addAction(UIAlertAction(title: "OK", style: .Default) { [unowned self, ac] _ in
            let newName = ac.textFields![0] as! UITextField
            person.name = newName.text

            self.collectionView.reloadData() })

        self.presentViewController(ac, animated: true, completion: nil)

    }))

    questionController.addAction(UIAlertAction(title: "Delete Person", style: .Default, handler: {

        (action:UIAlertAction!) -> Void in

        println("hello world")
        self.collectionView.deleteItemsAtIndexPaths([indexPath.item])
        self.collectionView.reloadData()

    }))

    presentViewController(questionController, animated: true, completion: nil)
}

"hello world" works okay but the app crashes when I press "Delete Person".

The console output is

hello world
2015-07-18 13:40:14.628 Project10[15888:1274436] -[__NSCFNumber section]:         unrecognized selector sent to instance 0xb000000000000003
2015-07-18 13:40:14.636 Project10[15888:1274436] *** Terminating app due to    uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber    section]: unrecognized selector sent to instance 0xb000000000000003'

What am I doing wrong?

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Alexey K
  • 6,537
  • 18
  • 60
  • 118

1 Answers1

27

You should change

self.collectionView.deleteItemsAtIndexPaths([indexPath.item])

to

self.collectionView.deleteItemsAtIndexPaths([indexPath])

deleteItemsAtIndexPaths expects an array of NSIndexPaths, not an array of numbers.

Besides that, if you call deleteItemsAtIndexPaths you don't need a call to reloadData - this will even prevent any animation from happening.

Don't forget to update your data source - the person has to be removed from the people array.

people.removeAtIndex(indexPath.item)

Do this before calling deleteItemsAtIndexPaths.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
  • 2
    didnt help Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of items in section 0. The number of items contained in an existing section after the update (1) must be equal to the number of items contained in that section before the update (1), plus or minus the number of items inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of items moved into or out of that section (0 moved in, 0 moved out).' – Alexey K Jul 18 '15 at 11:03
  • Don't forget to update your data source - the person has to be removed from the `people` array. – Glorfindel Jul 18 '15 at 11:06
  • sorry, im new in swift - how to update data source ? – Alexey K Jul 18 '15 at 11:08
  • i added self.people.removeAtIndex(indexPath.item) after the self.collectionView.deleteItemsAtIndexPaths([indexPath]) but it still crashes with same error – Alexey K Jul 18 '15 at 11:17
  • 1
    You need to do it *before*. Sorry if that was not clear. – Glorfindel Jul 18 '15 at 11:18
  • Your last line saved me a bunch of headache! Thanks a lot! – Shree Ranga Raju Jul 02 '20 at 22:41