ios - 如何删除节中的 TableView 行

标签 ios swift uitableview swift4

我有一个 tableview,其中包含 2 个部分,每个部分有多行。我正在尝试在点击按钮确认时删除一行。

原始数据是这样的

{
  "section one": [
    {},
    {}
  ],
  "section two": [
    {},
    {},
    {}
  ]
}

我创建了一组部分,其中每个部分都包含一组对象。一切都显示正确只是无法删除。

在我的服务中:

    var items = [Items]()
    var mySections = [MySection]()

在我看来:

    func numberOfSections(in tableView: UITableView) -> Int {
        return myService.mySections.count
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        let mySection = myService.mySections[section]
        return mySection.items.count
    }

我一直在研究这个,因为很多例子都表明了这一点:

    myService.mySections[indexPath.section].remove(at: indexPath.row)
    let indexPath = IndexPath(item: 0, section: 0)
    followupTable.deleteRows(at: [indexPath], with: .fade)

我似乎无法正确定位它,由于嵌套数据,我不能简单地使用 .remove。删除错误是 Value of type 'MySection' has no member 'remove'

最佳答案

鉴于 numberOfRowsInSection 中显示的数据结构,您删除行的代码需要如下所示:

myService.mySections[indexPath.section].items.remove(at: indexPath.row)
followupTable.deleteRows(at: [indexPath], with: .fade)

但这确实要求 itemsvar 而不是 let。因此,您可能需要更新数据结构以支持这一点。

另请注意,您不想从某个特定索引路径中删除项目,然后删除索引路径 0、0 处的单元格。您希望从相同的索引路径中删除。

关于ios - 如何删除节中的 TableView 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49702514/

相关文章:

swift - 使用多个搜索栏和交互式弹出手势时导航项 View 消失

objective-c - 插入没有任何动画的 UITableView 行

ios - 让 ViewControllers 访问 NavigationController 的好方法?

ios - 在 Xcode Cloud 配置屏幕中更改团队

swift - 快速获取 eaaccessory macaddress

arrays - 从 JSON Swift 获取数据

ios - iOS phone gap 发送邮件的问题

ios - 如何成功地将ImageMagick集成到iOS中?

ios - 在 Swift 中从 UITableView 中删除一个单元格

iOS 11 UITableViewCell 删除后 viewForHeaderInSection View 上方