Ios 行删除不';t更新表视图

Ios 行删除不';t更新表视图,ios,uitableview,swift3,realm,Ios,Uitableview,Swift3,Realm,我正在将数据加载到UITableViewController中,数据来自领域。因此,我的ViewController上有一个属性:var nextWeekPlants:Results 变量的加载方式如下:nextWeekPlants=realm.objects(Plant.self).filter(“…”)在我编写的updateUI()方法中。updateUI()方法还调用tableView.reloadData() 在我的numberOfRowsInSectiondelegate方法中,我有以

我正在将数据加载到UITableViewController中,数据来自领域。因此,我的ViewController上有一个属性:
var nextWeekPlants:Results

变量的加载方式如下:
nextWeekPlants=realm.objects(Plant.self).filter(“…”)
在我编写的
updateUI()
方法中。
updateUI()
方法还调用
tableView.reloadData()

在我的
numberOfRowsInSection
delegate方法中,我有以下检查:

if let plants = nextWeekPlants {
    return plants.count
} else {
    return 0
}
好的,它可以很好地收集数据,在屏幕上显示数据,但是一旦我从表视图中删除一个工厂并想删除另一个:它就会崩溃。
'rlmeException',原因:'索引1超出范围(必须小于1)

我以这种方式删除植物:

tableView.beginUpdates()
do {
    self.realm.beginWrite()
    self.realm.delete(self.nextWeekPlants[indexPath.row])
    try self.realm.commitWrite()
} catch (let error) {
    print(error.localizedDescription)
}
tableView.deleteRows(at: [indexPath], with: .left)
tableView.endUpdates()
它很好地删除了一株植物,但没有删除另一株。我是否必须以不同的方式更新tableView(调用
updateUI()
maybe?)还是需要更新我的领域集合

**编辑**

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

    // Little hack to show no extra cells and to make sure the swiping works.
    self.tableView.allowsMultipleSelectionDuringEditing = false
    self.tableView.tableFooterView = UIView()

    updateUI()
}

func updateUI() {
    let calendar = Calendar.current
    let maxDate = calendar.date(byAdding: .day, value: 7, to: Date())

    if let nextWeek = maxDate {
         nextWeekPlants = realm.objects(Plant.self).filter("nextWater <= %@", nextWeek)
    }

    self.tableView.setEditing(false, animated: true)
    self.tableView.reloadData()
}
覆盖函数视图将出现(u动画:Bool){
超级。视图将显示(动画)
//小黑客显示没有额外的细胞,并确保刷卡工程。
self.tableView.allowsMultipleSelectionDuringEdit=false
self.tableView.tableFooterView=UIView()
updateUI()
}
func updateUI(){
让calendar=calendar.current
让maxDate=calendar.date(通过将:.day,值:7添加到:date())
如果让nextWeek=maxDate{

nextWeekPlants=realm.objects(Plant.self).filter(“nextWater看起来您在不让realm知道的情况下改变UITableView状态。请参阅realm文档的一节

do {
    self.realm.beginWrite()
    self.realm.delete(self.nextWeekPlants[indexPath.row])
    tableView.deleteRows(at: [indexPath], with: .left)
    try self.realm.commitWrite(withoutNotifying: [token])
} catch (let error) {
    print(error.localizedDescription)
}

在结束更新之前,请尝试重新加载数据(),并从中删除该对象nextWeekPlants@jbehrens94我想您也需要更新表数组值。plants数组。@RJiryes这将更新tableView两次,我想这不是很好的UI。@TusharSharma所以我可能需要调用
updateUI()
再次,因为该方法设置了领域集合属性?请在调用
updateUI()的位置添加代码。
?是的,实际上,只需更新nextWeekPlants属性:-)