2017-10-08 35 views
0

我有一個viewController與tableView。當應用程序運行時,它崩潰的錯誤:行問題的應用程序崩潰(tableView,Swift)

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'attempt to insert row 0 into section 0, but there are only 0 rows in section 0 after the update'

Rhis是的viewController的部分代碼:

import UIKit 
import MapKit 
import CoreLocation 
import GoogleMaps 
import GooglePlaces 
import Social 
import AVFoundation 

private let resueIdentifier = "MyTableViewCell" 

extension UIViewController { 
    func present(viewController : UIViewController, completion : (() ->())? = nil){ 
     if let presented = self.presentedViewController { 
      presented.removeFromParentViewController() 
     } 
     self.present(viewController, animated: true, completion: completion) 
    } 
} 

class CourseClass2: UIViewController, UITableViewDelegate, UITableViewDataSource { 
    @IBOutlet weak var tableView: UITableView! 

    let minimumSpacing : CGFloat = 15 //CGFloat(MAXFLOAT) 
    let cellWidth: CGFloat = 250 
    var category : QCategoryy? 
    var places: [QPlace] = [] 
    var isLoading = false 
    var response : QNearbyPlacesResponse? 
    var rows = 0 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     self.title = category?.name 
    } 

    override func viewWillAppear(_ animated: Bool) { 
     super.viewWillAppear(animated) 

     determineMyCurrentLocation() 
    } 

    override func viewWillDisappear(_ animated: Bool) { 
     super.viewWillAppear(animated) 
    } 

    override func viewDidAppear(_ animated: Bool) { 
     super.viewDidAppear(animated) 
     rows = 0 
     tableView.reloadData() 
     insertRowsMode3() 
     category?.markView() 
    } 

    @IBAction func refreshTapped(_ sender: Any) { 
     rows = 0 
     tableView.reloadData() 
     insertRowsMode3() 
    } 

    func insertRowsMode2() { 
     for i in 0..<places.count { 
      insertRowMode2(ind: i, usr: places[i]) 
     } 
    } 

    func insertRowMode2(ind:Int,usr:QPlace) { 
     let indPath = IndexPath(row: ind, section: 0) 

     rows = ind + 1 
     tableView.insertRows(at: [indPath], with: .right) 
    } 

    func insertRowsMode3() { 
     rows = 0 
     insertRowMode3(ind: 0) 
    } 

    func insertRowMode3(ind:Int) { 
     let indPath = IndexPath(row: ind, section: 0) 
     rows = ind + 1 
     tableView.insertRows(at: [indPath], with: .right) 

     guard ind < places.count-1 else { return } 
     DispatchQueue.main.asyncAfter(deadline: .now()+0.20) { 
      self.insertRowMode3(ind: ind+1) 
     } 
    } 

    func numberOfSections(in tableView: UITableView) -> Int { 
     return 1 
    } 

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return places.count /* rows */ 
    } 

    public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! MyTableViewCell 

     let place = places[indexPath.row] 
     cell.update(place: place) 

     if indexPath.row == places.count - 1 { 
      loadPlaces(false) 
     } 

     return (cell) 
    } 

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
     tableView.deselectRow(at: indexPath, animated: true) 

     UIView.animate(withDuration: 0.2, animations: { 
      let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! MyTableViewCell 
     }) 

     performSegue(withIdentifier: "goToLast" , sender: users[indexPath.row]) 
    } 

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 
     return 100 
    } 

    func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool { 
     return true 
    } 

    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) { 
     if editingStyle == UITableViewCellEditingStyle.delete { 
      places.remove(at: indexPath.row) 
      tableView.deleteRows(at: [indexPath], with: .fade) 
     } 
    } 
} 

添加一個斷點,我看到飛機墜毀在此FUNC:

func insertRowMode3(ind:Int) { 
    let indPath = IndexPath(row: ind, section: 0) 
    rows = ind + 1 
    tableView.insertRows(at: [indPath], with: .right) 

    guard ind < places.count-1 else { return } 
    DispatchQueue.main.asyncAfter(deadline: .now()+0.20) { 
     self.insertRowMode3(ind: ind+1) 
    } 
} 
正是在這一行

tableView.insertRows(at: [indPath], with: .right) 

我知道必須先更新我的UITableViewDataSource,然後插入行,但我不知道如何以及如何修改才能解決問題。

回答

0

您必須在更改之前和之後使用開始&最終更新方法。

tableView.beginUpdates() 
// your modifications to the tableView 
tableView.endUpdates() 

它告訴tableView在此期間停止與實際tableView匹配的dataSource。

+0

所以我必須添加這個在我的func insertRows [2 | 3]? – seran

+0

詳細描述。在'beginUpdate'之後,你開始修改你的dataSource,通知tableView關於rowChanges(insert,delete ...),然後'endUpdate'提交你所有的修改,所以不會有任何不匹配。所以是的,把它放在這些功能中 – farzadshbfn

0

對於傳遞到insertRows的每個索引路徑,您需要將相應的對象添加到places陣列。在告訴表視圖關於行的任何變化之前,數據模型需要是最新的。

請注意,在您的commit editingStyle方法中,在正確更新places之前,請先告訴表視圖以刪除一行。

你的insertRowsMode[2|3]方法需要做同樣的事情,當然你需要增加一個值到places

相關問題