如何删除部分中的tableview行

时间:2018-04-07 00:08:31

标签: ios swift uitableview swift4

我有一个包含2个部分的tableview,每个部分有多行。点按按钮确认时,我正试图删除一行。

Orgininal数据看起来像这样

{
  "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。删除错误是“MySection”类型的值没有成员“删除”

2 个答案:

答案 0 :(得分:1)

要使您的问题发挥作用,您必须拥有一个像这样的数组

 var mySections = [["A","B","C"],["D","E","F"]]

此处mySections为[[String]],您可以[[MySection]]

然后你就可以了

mySections[indexPath.section].remove(at: indexPath.row)

答案 1 :(得分:1)

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

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

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

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