【发布时间】:2020-01-18 00:08:24
【问题描述】:
在更新数据源本地 JSON 文件时感到困惑。我在表格视图中显示了带有添加按钮的列表。我需要对按钮事件执行操作以在部分顶部添加特定行。我正在使用代表。基于分段的数据列表。
链接: https://drive.google.com/file/d/1cufp7hHNEVe4zZ7TiSCjFvFLm7EAWuXo/view?usp=sharing
extension DelegateViewController: DictionaryTableDelegate{
func didAddnewRow(_ tag: Int) {
print("Add Button with a tag: \(tag)")
AppList?.selectedValue?.append("Welcome")
let indexPath = IndexPath(row: AppData?.sectionList?.count ?? 0 - 1, section: 0)
tableView.beginUpdates()
tableView.insertRows(at: [indexPath], with: .automatic)
tableView.endUpdates()
tableView.reloadData()
}
错误: 尝试将第 3 行插入第 0 节,但更新后第 0 节只有 0 行
【问题讨论】:
-
与您的问题无关;不要使用开始/结束更新,这是用于批处理操作,您使用单个操作。插入后不要重新加载表格视图,它会为您执行此操作。
-
@Desdenova 在这两种情况下都不起作用。我已经尝试了很多。
Error : attempt to insert row 3 into section 0, but there are only 0 rows in section 0 after the update -
那是因为您没有更新数据源。您还需要将该数据插入到您的源中。
-
@Desdenova 是的,我该如何更新数据源?你能指导我吗?我试过但卡住了。
-
好的,我看到了你的项目。解码本地 json 后,创建一个
ListData对象并将其用作数据源。现在,当您点击“添加”按钮时,您需要获取indexPath.section和indexPath.row。 Section 将是您的SectionList位置的索引,Row 将是您的Item位置的索引。您需要创建一个新的Item对象并将其附加到您的数据源中。
标签: ios json swift uitableview