gpt4 book ai didi

swift - 为 Tableview 的扩展和折叠效果插入和删除行

转载 作者:行者123 更新时间:2023-11-28 13:51:27 24 4
gpt4 key购买 nike

我有一个关于数据源的问题。原因:“尝试将第 0 行插入第 0 节,但更新后第 0 节中只有 0 行”。

我一直在尝试展开和折叠我的表格 View 的第 1 部分。当我第一次看到 View Controller 时,我可以展开,然后折叠,但是当我第二次尝试展开它时,它崩溃了。当它在 numberOfRows 中扩展时,我尝试添加 + 1,但这也崩溃了。我知道我做错了什么以及我需要添加什么来完成这项工作。

编辑* 当我最初单击以展开该部分时,在 numberofRowsInSection 中运行 if isExpanded == false 语句,给我一个 section.count - 1。但为什么运行并给我返回一行?看来我的问题与此有关,但我知道修复方法。

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
}

最佳答案

对于 future 的解决方案寻求者,这是插入和删除行以使其可折叠的方法。

 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
}

扩展 View Controller :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()
}

关于swift - 为 Tableview 的扩展和折叠效果插入和删除行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54556928/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com