错误:无法从tableview中删除单元格

时间:2015-10-26 07:07:11

标签: swift tableview ios9

  

由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'无效更新:第0节中的行数无效。更新后现有部分中包含的行数...

我已经尝试了几乎所有解决方案,我在互联网上找到了 - 实际上并没有帮助。

这是我的代码:

import Foundation
import UIKit
class ViewController3: UIViewController, UITableViewDelegate,     UITableViewDataSource {

    var coordinatesDictionaryInfo: [String : AnyObject] = [
        "" : ""]

    var arrayOfCoordinates = [String]()
    var arrayOfInfromation = [AnyObject]()

    func dictionaryToArray() {
        for (key, item) in coordinatesDictionaryInfo {
            arrayOfCoordinates.append(key)
            arrayOfInfromation.append(item)
        }
    }

    override func viewDidLoad() {
        self.table.reloadData()
    }

    @IBOutlet weak var table: UITableView!

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        dictionaryToArray()
        return self.arrayOfCoordinates.count
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cellIdentifier = "Cell"
        let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier,
            forIndexPath: indexPath)

        // Configure the cell...
        cell.detailTextLabel!.text = arrayOfInfromation[indexPath.row] as? String
        cell.textLabel!.text = arrayOfCoordinates[indexPath.row]
        return cell
    }

    func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
        return true
    }

    func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
        if editingStyle == UITableViewCellEditingStyle.Delete {
            tableView.beginUpdates()
            self.arrayOfCoordinates.removeAtIndex(indexPath.row)
            self.arrayOfInfromation.removeAtIndex(indexPath.row)
            tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Fade)
            tableView.endUpdates()
        }
    }

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        let DVC = segue.destinationViewController as! ViewController
        DVC.coordinatesDictionary = coordinatesDictionaryInfo
        //отправляем словарь на второй ViewController2
    }
}

0 个答案:

没有答案
相关问题