致命错误:数组索引超出范围

时间:2022-11-20 16:42:09

My Situation:

我的情况:

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

在AppDelegate.swift中有一个名为friendCardList的数组,而CardModel只是普通的类:

var friendCardList:[CardModel] = []

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

在名为FriendListVC的UITableViewController中,数组friendCardList中的元素将在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:

我试图删除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

致命错误:数组索引超出范围

I blocked up here all the noon.

我整个中午都在这里*了。

1 个解决方案

#1


7  

You need to call

你需要打电话

self.reloadData()

in your UITableViewController each time you modify your data source.

每次修改数据源时,在UITableViewController中。

#1


7  

You need to call

你需要打电话

self.reloadData()

in your UITableViewController each time you modify your data source.

每次修改数据源时,在UITableViewController中。