2016-09-08 4 views
0

最初のアプリの簡単な説明を修正する方法:わからないNSInconsistencyException

アプリは、彼らが(犬のデイケア会社の)建物に現在あるかどうかの犬のリストを表示して示すことが意図されている

。ユーザーは犬を検索して追加します。アプリはこれらの犬をデータベースから取得します。

私はこれを一点で扱っていました。しかし、バックグラウンドスレッドで実行されている自動レイアウトに関するエラーが発生しました。私はそれを見つけて、この行が見つかりました: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() 
} 

エラー:

*** Assertion failure in -[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 Day Care Register[1961:654300] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (3) must be equal to the number of rows contained in that section before the update (0), plus or minus the number of rows inserted or deleted from that section (1 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'

私は右、それはより速く、それが作成していますよりも私のアレイにDog Sを追加して機能を語っていますことを理解していればテーブルの行?

誰でもこの問題を解決する手助けはできますか?

答えて

1

あなたが提供している配列のサイズが、更新しているのと同じでない場合、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() 
}) 
+0

おかげで、この問題を修正しました。私は別のキーを押すと、エラーが発生する、私は原因を知っている。テーブルからすべての行をクリアする関数はありますか? - それとも自分で書かなければなりませんか? –

+0

nvm、私はそれを得た。私の元の質問を助けてくれてありがとう:) –

関連する問題