【问题标题】:How to remove data from array on click of close button单击关闭按钮如何从数组中删除数据
【发布时间】:2019-02-17 07:40:46
【问题描述】:

我有一个数组,我需要在单击“关闭”时添加和删除单元格数据,这是标题部分中的 UIButton

我正在循环数组并获取行,对于部分我使用了静态数据并在 indexPaths 中添加,然后在

tblSideNav.deleteRows(at: indexPaths, with: .fade) 

如上一行 start execution app got crash

 var menusArray = [sideNavStruct(isOpend: true, menuImg: "user", menuTitle: "Profile", subMenu: ["My Profile", "Distributor Profile"]), sideNavStruct(isOpend: true, menuImg: "user", menuTitle: "Reports", subMenu: ["Stock Report", "NFR Report", "RTD”])]

    @objc func handleOpenCloseCell() {
    let section = 0
    var indexPaths = [IndexPath]()
    for row in menusArray[section].subMenu.indices {
        let indexPath = IndexPath(row: row, section: section)
        indexPaths.append(indexPath)
    }
    print(indexPaths.count)
    print(menusArray[section].subMenu[0])
    menusArray[section].subMenu[0].removeAll()
    tblSideNav.deleteRows(at: indexPaths, with: .fade)
}

我的应用在tblSideNav.deleteRows(at: indexPaths, with: .fade) 上崩溃了:

无效更新:第 0 节中的行数无效。 更新 (7) 后包含在现有节中的行必须是 等于该节之前包含的行数 update (2),加或减插入或删除的行数 该部分(0 插入,2 删除)和加或减的数量 移入或移出该部分的行(0 移入,0 移出)。

我找不到问题

【问题讨论】:

  • 崩溃说明了什么?
  • Labwa @AhmadF ...
  • @AhmadF 原因:'无效更新:第 0 节中的行数无效。更新后现有节中包含的行数 (7) 必须等于其中包含的行数更新前的节 (2),加上或减去从该节插入或删除的行数(0 插入,2 删除),加上或减去移入或移出该节的行数(0 移入,0 移动出)。'

标签: ios swift uitableview uitableviewsectionheader


【解决方案1】:

首先,您调用 deleteRows 时没有必需的 beginUpdates 和 endUpdates。

tableView.beginUpdates()
tableView.deleteRows(at: indexPaths, with: .fade)
tableView.endUpdates()

如果您的委托函数在更新后没有返回正确的行数,也会发生同样的错误。

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

我相信更好的做法是更改/清空存储行信息/数据的变量,然后简单地调用 reloadData()。

【讨论】:

  • @SanjayMishra 由于错误消息,我认为您确实在 NumberOfRowsInsection 委托函数中返回了错误的行数。你应该检查一下。如果您能提供更多上下文,我可以仔细查看您的代码。
  • @Mateh Simota ti return numberOfRowInSection 我使用了下面的代码,一旦我使用正确或错误,您可以检查它。 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { if menusArray[section].isOpend == true { print(menusArray[section].subMenu.count) if menusArray[section].subMenu.count > 0 { return menusArray[section].subMenu.count } else { return menusArray[section].menuTitle.count } } else { return 0 } }
  • @SanjayMishra 我现在看到了问题...当 subMenu.count == 0 时,您返回 "return menusArray[section].menuTitle.count" ,这意味着您返回的字母数你的题目。只需删除整个内部 if 语句。 if menusArray[section].isOpend == true { return menusArray[section].subMenu.count } else { return 0 }
  • @Mates Simota 现在给出错误“NSInternalInconsistencyException”,原因:“尝试从第 0 部分删除第 1 行,更新前仅包含 0 行”
猜你喜欢
  • 2021-02-06
  • 1970-01-01
  • 2018-06-08
  • 1970-01-01
  • 2021-12-01
  • 2020-04-16
  • 1970-01-01
  • 2016-01-04
  • 2022-01-23
相关资源
最近更新 更多