Swift 插入&;删除要展开的行(&A);Tableview的折叠效果
我有一个关于数据源的问题。 原因:“尝试将行0插入节0,但更新后节0中只有0行。” 我一直在尝试扩展和折叠tableview的第1部分。当我第一次看到视图控制器时,我可以展开,然后折叠,但是当我第二次尝试展开它时,它崩溃了。我尝试将+1添加到numberOfRows中,但这也会崩溃。idk我做错了什么,我需要补充什么才能让这一切顺利进行 Edit*当我最初单击展开节时,在numberofRowsInSection内,if isExpanded==false语句运行,给我一个section.count-1。但那为什么会跑回来和我吵架呢?我的问题似乎与此有关,但我没有找到解决办法Swift 插入&;删除要展开的行(&A);Tableview的折叠效果,swift,uitableview,Swift,Uitableview,我有一个关于数据源的问题。 原因:“尝试将行0插入节0,但更新后节0中只有0行。” 我一直在尝试扩展和折叠tableview的第1部分。当我第一次看到视图控制器时,我可以展开,然后折叠,但是当我第二次尝试展开它时,它崩溃了。我尝试将+1添加到numberOfRows中,但这也会崩溃。idk我做错了什么,我需要补充什么才能让这一切顺利进行 Edit*当我最初单击展开节时,在numberofRowsInSection内,if isExpanded==false语句运行,给我一个section.cou
var sectionArray = [ ExpandableCell(isExpanded: false, section: [""])
]
@objc func handleExpandClose(button: UIButton) {
let indexPath = IndexPath(row: 0, section: 0)
let isExpanded = sectionArray[0].isExpanded
if isExpanded {
sectionArray[0].section.removeAll()
tableView.beginUpdates()
tableView.deleteRows(at: [indexPath], with: .fade)
tableView.endUpdates()
} else {
sectionArray[0].section.append("")
tableView.beginUpdates()
tableView.insertRows(at: [indexPath], with: .fade)
tableView.endUpdates()
}
sectionArray[0].isExpanded.toggle()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if section == 0 && sectionArray[0].isExpanded {
return sectionArray[0].section.count
} else if section == 0 && sectionArray[0].isExpanded == false {
return sectionArray[0].section.count - 1
}
else if section == 1 {
return 1
}
return 0
}
当应用程序运行此
if section == 0 && sectionArray[0].isExpanded == false
根据[0].section.count-1运行,使行数为0,然后当您单击操作手柄ExpandClose时,else运行
} else {
sectionArray[0].section.append("")
tableView.beginUpdates()
tableView.insertRows(at: [indexPath], with: .fade)
在其中,您将数据附加到唯一对象内部的内部数组,因此在插入时,数据源主数组sectionArray没有更改,因此会发生崩溃
谢谢,那么这个问题的解决办法是什么呢?
class TableViewController: UITableViewController {
var sectionArray = [ExpandableCell(),ExpandableCell(),ExpandableCell()]
override func viewDidLoad() {
super.viewDidLoad()
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem
self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
// simulate collapse action
DispatchQueue.main.asyncAfter(deadline: .now() + 4) {
self.sectionArray[0].isExpanded = false
self.tableView.reloadData()
}
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return sectionArray.count
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return sectionArray[section].isExpanded ? sectionArray[section].content.count : 0
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
// Configure the cell...
cell.textLabel?.text = sectionArray[indexPath.section].content[indexPath.row]
return cell
}
}
struct ExpandableCell {
var isExpanded = true
var content = ["1","2","3"]
}