首页 > 解决方案 > 删除 UITableview 崩溃应用程序中的部分

问题描述

我有一个 UITableView,我想删除部分和行。我正在尝试删除其中包含行的整个部分。以下是让我感到困惑的案例。

  1. 当我尝试从上到下或从下到上删除部分时,所有部分都会被删除而不会出错
  2. 当我尝试随机删除部分时,即首先我删除顶部的第一部分,然后当我尝试删除第 4 或第 8 或最后一个部分时,应用程序会自行崩溃

崩溃日志

致命错误:索引超出范围

这是我的做法:

 func onHeaderSwippedRight(ClickedSection: IndexPath) {


    dataSource.remove(at: ClickedSection.section)

    // use the UITableView to animate the removal of this row
    tableView.beginUpdates()

    tableView.deleteSections(IndexSet.init(integer : ClickedSection.section), with: .automatic)

    tableView.endUpdates()

}

我传递给此函数onHeaderSwippedRight(ClickedSection: IndexPath)的索引路径由委托方法传递。Bcz 在我的课堂上,我在设置我的部分数据时传递它

我的一个原始猜测是,在删除部分 tableview 之后必须更新部分的索引,因为当我从数据源中删除数据时,如果部分自己更新,它一定不会让 Index out of bound 问题

请帮助并告诉我我做错了什么

更新;

public class ListModel : Object {


var   Id : String = ""
var   UserId : Int64 = 0
var   Name : String? = ""
var   Items : [ListDetailServiceModel]? =  []}

这是我的模型,我有这样的列表

let dataSource :[ListModel] = []

标签: iosuitableviewswift4xcode9

解决方案


推荐阅读