Yichen Zhou
Yichen Zhou

Reputation: 446

Fatal error: Array index out of range

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.

Upvotes: 0

Views: 2041

Answers (1)

Sunkas
Sunkas

Reputation: 9590

You need to call

self.reloadData()

in your UITableViewController each time you modify your data source.

Upvotes: 7

Related Questions