我有关于数据源的问题。原因:"尝试将第 0 行插入第 0 部分,但更新后第 0 部分只有 0 行"。
我一直在尝试展开和折叠表格视图的第 1 部分。当我第一次看到视图控制器时,我可以展开,然后折叠,但是当我第二次尝试展开它时,它崩溃了。我尝试在行数扩展时添加 + 1,但这也崩溃了。idk 我做错了什么以及我需要添加什么才能完成这项工作。
编辑* 当我最初单击以展开该部分时,在行数内运行 if isExpanded == false 语句,给我一个部分.count - 1。可是,为什么跑了,还给我一排呢?似乎我的问题与此有关,但 IDK 修复。
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
}
对于未来的解决方案寻求者,以下是插入和删除行以使可折叠的方法。
class ViewController: UIViewController {
let navigationBar: UINavigationBar = {
let width = UIScreen.main.bounds.width
let navBar = UINavigationBar(frame: CGRect(x: 0, y: 0, width: width, height: 25))
let navItem = UINavigationItem(title: "Lists")
let addIcon = UIBarButtonItem(systemItem: .add)
navItem.rightBarButtonItem = addIcon
navBar.items = [navItem]
return navBar
}()
let tableView: UITableView = {
let table = UITableView(frame: .zero, style: .insetGrouped)
table.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
return table
}()
var isOpen: Bool = false
var list = ["Fruites", "Vegetable"]
var fruits = ["Apple", "Orange", "Banana", "Mango", "Pineapple"]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
tableView.delegate = self
tableView.dataSource = self
view.addSubview(navigationBar)
view.addSubview(tableView)
setupConstraints()
}
private func setupConstraints() {
navigationBar.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 0).isActive = true
navigationBar.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
navigationBar.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
navigationBar.translatesAutoresizingMaskIntoConstraints = false
tableView.topAnchor.constraint(equalTo: navigationBar.bottomAnchor).isActive = true
tableView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
tableView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
tableView.translatesAutoresizingMaskIntoConstraints = false
}
}
extension ViewController: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return list.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell",for: indexPath)
cell.textLabel?.text = list[indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if !isOpen {
for i in 0..<fruits.count {
/// Here i am inserting at 1, you can insert where ever you want
list.insert(fruits[i], at: 1+i)
tableView.insertRows(at: [.init(row: 1+i, section: 0)], with: .fade)
}
} else {
for _ in 0..<fruits.count {
list.removeLast()
print(list.last!)
print( list.count)
tableView.deleteRows(at: [.init(row: list.count - 1, section: 0)], with: .fade)
}
}
isOpen.toggle()
}
}
当应用程序运行此内容时
if section == 0 && sectionArray[0].isExpanded == false
运行所以根据ectionArray[0].section.count - 1
行数为0,然后当你点击动作句柄展开关闭时,否则运行
} else {
sectionArray[0].section.append("")
tableView.beginUpdates()
tableView.insertRows(at: [indexPath], with: .fade)
在其中你把数据追加到内部数组里面的唯一对象,所以当你插入的时候,dataSource主数组section数组没有改变,因此崩溃了
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"]
}