【问题标题】:Not sure how to fix NSInconsistencyException不确定如何修复 NSInconsistencyException
【发布时间】:2016-09-08 21:17:39
【问题描述】:

我正在开发的应用程序旨在显示狗列表并指出它们当前是否在建筑物中(对于狗日托公司)。用户通过搜索添加了一条狗。该应用程序从数据库中获取这些狗。

我曾经有过这个工作。但是,我在后台线程上运行自动布局时遇到错误。我用谷歌搜索,发现这一行:dispatch_async(dispatch_get_main_queue(), {}) 修复了错误。

问题是我现在有一个NSInconsistencyException

导致错误的函数:

 func searchFor(){
    //print(searchBar.text)
    let url = NSURL(string: "http://192.168.0.8/classes/main.php?fn=dogSearch&s=" + searchBar.text!)
    let task = NSURLSession.sharedSession().dataTaskWithURL(url!) {(data, response, error) in
        print(NSString(data: data!, encoding: NSUTF8StringEncoding))
        //var boardings = [String]()
            self.dogs.removeAll()
        do {
            let json = try NSJSONSerialization.JSONObjectWithData(data!, options: .AllowFragments) as! [AnyObject]

            if let theDogs = json as? [[String: AnyObject]] {
                for dog in theDogs {
                    if let ID = dog["ID"] as? String {
                        print(ID + " Safe")
                        let thisDog = Dog(name: (dog["Name"] as? String)!, surname: (dog["Surname"] as? String)!, id: (dog["ID"]  as? String)!, boarding: false)
                        let newIndexPath = NSIndexPath(forRow: self.dogs.count, inSection: 0)
                        self.dogs.append(thisDog)
                        dispatch_async(dispatch_get_main_queue(), {
                            // code here
                        self.tableView.insertRowsAtIndexPaths([newIndexPath], withRowAnimation: .Bottom)
                        })
                    }
                }
            }
        } catch {
            print("error serializing JSON: \(error)")
        }

        // print(names) // ["Bloxus test", "Manila Test"]

    }

    task.resume()
}

错误:

*** -[UITableView _endCellAnimationsWithContext:] 中的断言失败, /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit- 3512.60.7/UITableView.m:1716
2016-09-08 21:56:27.559 日托登记[1961:654300] *** 由于未捕获的异常而终止应用程序 'NSInternalInconsistencyException',原因:'无效更新:无效 第 0 节中的行数。包含在第 0 节中的行数 更新后的现有节(3)必须等于 更新之前该部分中包含的行 (0),加上或减去 从该部分插入或删除的行数(1 插入,0 已删除)并加上或减去移入或移出的行数 部分(0 移入,0 移出)。'

如果我理解正确,它告诉我该函数将Dogs 添加到数组中的速度比它为表创建行的速度要快吗?

【问题讨论】:

    标签: ios swift uitableview


    【解决方案1】:

    如果您提供给 UITableView 的数组与您更新它时的大小不同,UITableView 将引发异常。

    为防止这种情况发生,请将您的代码封装在 beginUpdates()endUpdates() 调用中,这样当您将狗添加到数组后,您也可以将所有行添加到同一批次中的表中。

    尝试将整个操作放在主队列中并将其包装在这些更新调用中,如下所示:

    dispatch_async(dispatch_get_main_queue(), {
        self.tableView.beginUpdates()
        if let theDogs = json as? [[String: AnyObject]] {
            for dog in theDogs {
                if let ID = dog["ID"] as? String {
                    print(ID + " Safe")
                    let thisDog = Dog(name: (dog["Name"] as? String)!, surname: (dog["Surname"] as? String)!, id: (dog["ID"]  as? String)!, boarding: false)
                    let newIndexPath = NSIndexPath(forRow: self.dogs.count, inSection: 0)
                    self.dogs.append(thisDog)
    
    
                    self.tableView.insertRowsAtIndexPaths([newIndexPath], withRowAnimation: .Bottom)
                }
            }
        }
        self.tableView.endUpdates()
    })
    

    【讨论】:

    • 谢谢,这解决了问题。但是出现了一个不同的问题,当我按下一个键时它工作正常,当我按下另一个键时,发生错误,我知道原因。是否有清除表中所有行的功能? -- 还是我必须自己写?
    • nvm,我明白了。感谢您帮助解决我最初的问题:)
    猜你喜欢
    • 2017-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-23
    相关资源
    最近更新 更多