0

My Situation:

There is an array called friendCardList claimed in the AppDelegate.swift, and the CardModel is just normal class :

var friendCardList:[CardModel] = []

In a UITableViewController called FriendListVC, the elements in the array friendCardList will be listed in the FriendListVC:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let delegate = UIApplication.sharedApplication().delegate as! AppDelegate
    let card = delegate.exchangeListCards[indexPath.row]
    let cell = tableView.dequeueReusableCellWithIdentifier("CardsCell",
    forIndexPath: indexPath) as! BrowserTableViewCell
    return cell
}

I was trying to delete the cell in the FriendListVC:

func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
    let delegate = UIApplication.sharedApplication().delegate as! AppDelegate
    let card = delegate.exchangeListCards[indexPath.row]
    var CardSet = NSMutableSet(array: delegate.exchangeListCards)
    CardSet.removeObject(card)
    delegate.exchangeListCards = CardSet.allObjects as! [CardModel]
} 

My Problem:

When I deleted the cell through sliding the cell to the left, the complier thrown the bug:

fatal error: Array index out of range

enter image description here

I blocked up here all the noon.

2
  • 6
    Hint: You removed the data from array, but your tableview doesn't know that. You need to inform your tableview about the dataset change.!!! Commented May 4, 2015 at 9:49
  • @MidhunMP I'm dealing with a similar error, how do you inform the tableview of the change? I have used self.reloadData() in my code Commented Sep 4, 2015 at 14:59

1 Answer 1

7

You need to call

self.reloadData()

in your UITableViewController each time you modify your data source.

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

2 Comments

Thank you very much. Bug went away when I added self.reloadData( ).
@EthanJoe: I won't recommend this. Reloading entire tableview is not a good practice. You can use deleteRowAtIndexPaths method instead. Check this tutorial ioscreator.com/tutorials/delete-rows-from-tableview

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.