tableView.endUpdates()后发生错误

时间:2017-12-21 10:57:58

标签: ios swift uitableview realm

我试图设置一个断点,我注意到tableView.endUpdates后出现错误

  

由于未捕获的异常而终止应用   ' NSInternalInconsistencyException',原因:'无效更新:无效   第0节中的行数。包含在中的行数   更新后的现有部分(333)必须等于数量   更新前的该部分中包含的行(333),加号或减号   从该部分插入或删除的行数(插入0,   235删除)并加上或减去移入或移出的行数   该部分(0移入,0移出)。'

import UIKit
import RealmSwift
import RxCocoa
import RxSwift
var myIndex = 0
class TCB_View: UIViewController,UITableViewDelegate,UITableViewDataSource,UISearchBarDelegate{

    var notificationToken: NotificationToken? = nil
    @IBOutlet weak var searchBar: UISearchBar!
    @IBOutlet weak var tableView: UITableView!
    var filteredArray:[String] = []
    var branchesArray:[String] = []
    var compositeDisposable = CompositeDisposable()
    var isSearchin = false
    deinit {
        compositeDisposable.dispose()
        notificationToken?.invalidate()
    }
    var count = 0
    override func viewDidLoad() {
        super.viewDidLoad()

        tableView?.delegate = self
        tableView?.dataSource = self
        searchBar?.delegate = self
        searchBar?.returnKeyType = UIReturnKeyType.done
        searchBar?.placeholder = "Search Trial Courts"

        let realm = try! Realm()
        let tcb = realm.objects(TrialCourtBranches.self).filter("tc.tc_filter == true")
        count = tcb.count
        print(count)
        for branch in tcb{
                branchesArray.append(branch.branch_name)

        }
        notificationToken = tcb.observe { [weak self] (changes: RealmCollectionChange) in
            guard let tableView = self?.tableView else { return }
            switch changes {
            case .initial:
                // Results are now populated and can be accessed without blocking the UI
                tableView.reloadData()
            case .update(_, let deletions, let insertions, let modifications):
                let fromRow = {(row: Int) in return IndexPath(row: row,section:0) }
                // Query results have changed, so apply them to the UITableView
                tableView.beginUpdates()
                tableView.insertRows(at: insertions.map(fromRow),with: .automatic)
                tableView.deleteRows(at: deletions.map(fromRow),with: .automatic)
                tableView.reloadRows(at: modifications.map(fromRow),with: .automatic)
                tableView.endUpdates()
            case .error(let error):
                // An error occurred while opening the Realm file on the background worker thread
                fatalError("\(error)")
            }
        }
    }

    func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
        if searchBar.text == nil || searchBar.text == ""{
            isSearchin = false
            view.endEditing(true)
            tableView.reloadData()
        }else{
            isSearchin = true
            compositeDisposable.insert(UISearchBar.rx.init(searchBar).value.subscribe(onNext: { (stringResult) in
                self.filteredArray = self.branchesArray.filter({(branchesArray:String) -> Bool in
                    if branchesArray.contains(self.searchBar.text!){
                        return true
                    }else{
                        return false
                    }
                })
            }, onError: { (errorResult) in
                print(errorResult)
            }, onCompleted: {
                print("onCompleted")
            }, onDisposed: {
                print("onDisposed")
            }))
            tableView.reloadData()
        }
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return count
    }

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

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "TrialCourtCell") as! CustomTableViewCell
            cell.branchName.text = branchesArray[indexPath.row]
        return cell
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        myIndex = indexPath.row
    }
}

当我在另一个viewcontroller中点击tableCell时,执行错误

0 个答案:

没有答案