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

标签 swift uitableview

我有一个关于数据源的问题。 原因:“尝试将第 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/

相关文章:

iphone - 如何计算通过 cellForRowAtIndexPath 中的开关设置的单元格的 heightForRowAtIndexPath。

iphone - 更新 iPhone 上表格单元格中的值

iphone - 在 iPhone 上使 UIImageView 不透明时出现问题

ios - 全局是外部的,但没有外部或弱联系

swift - 如何在 Swift 中为 UIViewController 子类创建自定义初始值设定项?

ios - 如何使用服务器中的数据创建多个 MapKit 注释?

swift - CoreData 在用户数量较少时崩溃

ios - 如何将 UITableViewCell 高度调整为内部 UITextView 的内容?

ios - @EnvironmentObject 可以与不是 View 的普通结构一起使用吗?

ios - 在调用 reloadData 之前,Autolayout 会错误地调整 UITableViewCells 的大小