我有一个 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)
但这确实要求 items
是 var
而不是 let
。因此,您可能需要更新数据结构以支持这一点。
另请注意,您不想从某个特定索引路径中删除项目,然后删除索引路径 0、0 处的单元格。您希望从相同的索引路径中删除。
关于ios - 如何删除节中的 TableView 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49702514/