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

Eth*_*Joe 0 arrays ios swift

我的情况:

有一个叫做friendCardList声明的数组AppDelegate.swift,而且CardModel只是普通的类:

var friendCardList:[CardModel] = []
Run Code Online (Sandbox Code Playgroud)

在UITableViewController调用中FriendListVC,数组中的元素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
}
Run Code Online (Sandbox Code Playgroud)

我试图删除以下单元格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]
} 
Run Code Online (Sandbox Code Playgroud)

我的问题:

当我通过向左滑动单元格来删除单元格时,编译器抛出了错误:

fatal error: Array index out of range
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

我整个中午都在这里封锁了.

Sun*_*kas 7

你需要打电话

self.reloadData()
Run Code Online (Sandbox Code Playgroud)

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

  • @EthanJoe:我不会推荐这个.重新加载整个tableview不是一个好习惯.您可以使用deleteRowAtIndexPaths方法.查看本教程http://www.ioscreator.com/tutorials/delete-rows-from-tableview (3认同)