致命错误:数组索引超出范围
我的情况:致命错误:数组索引超出范围
有一个称为friendCardList
要求中的AppDelegate.swift
阵列,并且所述CardModel
只是正常类:
var friendCardList:[CardModel] = []
在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
}
我试图删除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]
}
我的问题:
当我通过细胞向左滑动删除单元格时,编译器抛出的错误:
fatal error: Array index out of range
我堵住了这里所有的中午。
你需要调用
self.reloadData()
在你的UITableViewController
每次修改您的数据源。
中使用了self.reloadData()非常感谢。当我添加'self.reloadData()'时,Bug就消失了。 –
@EthanJoe:我不会推荐这个。重新加载整个tableview并不是一个好习惯。您可以改用deleteRowAtIndexPaths方法。查看本教程http://www.ioscreator.com/tutorials/delete-rows-from-tableview –
提示:你从数组中删除了数据,但是你的tableview并不知道。你需要通知你的tableview关于数据集的变化。 –
@MidhunMP我正在处理类似的错误,你如何通知tableview的变化?我在我的代码 – SamYoungNY