Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何删除Swift中节的所有行_Ios_Swift - Fatal编程技术网

Ios 如何删除Swift中节的所有行

Ios 如何删除Swift中节的所有行,ios,swift,Ios,Swift,我正在编写一个代码,在这个代码中,我必须在选择一个节时插入和删除所有行 下面是插入和删除的代码 var paths = [IndexPath]() for row in 0..<(arrayOfSection?.count)! { let indexPath = IndexPath(row: row, section: selectedIndex!) paths.append(indexPath) } // Toggle collapse if previousSelec

我正在编写一个代码,在这个代码中,我必须在选择一个节时插入和删除所有行

下面是插入和删除的代码

var paths = [IndexPath]()
for row in 0..<(arrayOfSection?.count)! {
    let indexPath = IndexPath(row: row, section: selectedIndex!)
    paths.append(indexPath)
}

// Toggle collapse
if previousSelectedIndex != nil && previousSelectedIndex == section {

    self.arrayOfCategories?.removeAll()
    self.catergoryTableView.beginUpdates()
    self.catergoryTableView.deleteRows(at: paths, with: .fade)
    self.catergoryTableView.endUpdates()
}
else{
    self.catergoryTableView.beginUpdates()
    self.catergoryTableView.insertRows(at: paths, with: .fade)
    self.catergoryTableView.endUpdates()
    header.setCollapsed(false)
    //scroll
    let sectionRect = self.catergoryTableView.rect(forSection: selectedIndex!)
    self.catergoryTableView.scrollRectToVisible(sectionRect, animated: true)
}

previousSelectedIndex = selectedIndex
var路径=[IndexPath]()

对于0..中的行,更新代码以删除所有行,如下所示:

self.catergoryTableView.beginUpdates()
self.catergoryTableView.deleteRows(at: paths, with: .fade)
self.catergoryTableView.endUpdates()
self.arrayOfCategories?.removeAll()

只需将第一行代码添加到最后一行。

不相关,但与往常一样,
beginUpdates
/
endUpdates
对于单个惰性/移动/删除操作完全无效。在调用
insertRows/deleteRows
之前,必须在数据源中插入/删除相应的项。如果删除数据源,则不能一次插入、删除一行。重新加载表或删除相应索引处的数据源,每次删除一个。您应该尝试删除以下代码
self.arrayOfCategories?.removeAll()
。是否尝试在单击的节上展开和折叠行?如果需要,可以删除整个节