【问题标题】:Update JSON data source on button click tableview swift在按钮单击 tableview swift 上更新 JSON 数据源
【发布时间】: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.sectionindexPath.row。 Section 将是您的SectionList 位置的索引,Row 将是您的Item 位置的索引。您需要创建一个新的Item 对象并将其附加到您的数据源中。

标签: ios json swift uitableview


【解决方案1】:

我已经看到您的项目,需要进行一些更改,以便从底部添加选定的项目以添加到顶部。

首先更新您的DictionaryTableDelegate 方法如下:

protocol DictionaryTableDelegate {
    func didAddnewRow(_ sender: UIButton)
}

然后如下更改委托调用。

@IBAction func addClicked(_ sender: UIButton) {
    delegate?.didAddnewRow(sender)
}

itemslet 更改为 var

struct SectionList : Codable {
    let title : String?
    var items : [Item]?
}

同样,将 sectionListlet 更改为 var

struct ListData : Codable {
    var sectionList : [SectionList]?    
}

更新didAddnewRow的代码如下将解决您的问题:

extension DelegateViewController: DictionaryTableDelegate{

    func didAddnewRow(_ sender: UIButton) {

        if let cell = sender.superview?.superview as? DictionaryTableViewCell,
            let indexPath = self.tableView.indexPath(for: cell)
        {
            if let selectedItem = AppData?.sectionList?[indexPath.section].items?[indexPath.row] {

                let insertIndexPath = IndexPath(item: AppData?.sectionList?[0].items?.count ?? 0, section: 0)

                AppData?.sectionList?[0].items?.append(selectedItem)

                tableView.beginUpdates()
                tableView.insertRows(at: [insertIndexPath], with: .automatic)
                tableView.endUpdates()
            }
        }
    }
}

如果您想从底部删除选定的行,请更新以下代码

func didAddnewRow(_ sender: UIButton) {

    if let cell = sender.superview?.superview as? DictionaryTableViewCell,
        let indexPath = self.tableView.indexPath(for: cell),
        indexPath.section != 0
    {
        if let selectedItem = AppData?.sectionList?[indexPath.section].items?[indexPath.row] {

            let insertIndexPath = IndexPath(item: AppData?.sectionList?[0].items?.count ?? 0, section: 0)

            AppData?.sectionList?[0].items?.append(selectedItem)
            AppData?.sectionList?[indexPath.section].items?.remove(at: indexPath.row)

            tableView.beginUpdates()
            tableView.insertRows(at: [insertIndexPath], with: .automatic)
            tableView.deleteRows(at: [indexPath], with: .automatic)
            tableView.endUpdates()
        }
    }
}

【讨论】:

  • 首先谢谢你,我已经做出了相应的改变,但是得到了reason: 'attempt to insert row 0 into section 0, but there are only 0 rows in section 0 after the update让我分享更新的代码链接:drive.google.com/file/d/1maRuKe8KEH9Fw2PgVSWsOtX3iETshwTS/…
  • 完成了,稍微调整一下就完成了。非常感谢您的支持。
猜你喜欢
  • 1970-01-01
  • 2012-04-07
  • 1970-01-01
  • 2014-01-17
  • 1970-01-01
  • 1970-01-01
  • 2021-08-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多