首页 > 解决方案 > tableview 单元格添加说明第 0 节中的无效行数

问题描述

每次我点击添加按钮单元格。当用户点击添加按钮时,应立即将一个新单元格添加到 tableview 中,整个代码将进入运行时错误。我想修复该错误,以便您可以向 tableview 添加一个单元格。运行时错误状态

"无效更新:第 0 节中的行数无效。更新后现有节中包含的行数 (5) 必须等于更新前该节中包含的行数 (5),加上或减去从该部分插入或删除的行数(1 插入,0 删除)加上或减去移入或移出该部分的行数(0 移入,0 移出)。

import UIKit
 

 
class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
 
    

    var addBTN = UIButton()
    var arr = [1,1,3,3]
    var currentIndex = 0
    var arrayValues = [Int]()
    
   
    

 
    var pic = UIImageView()
    var addBtn = UIButton()
    var cellCount = 5
    var tableview = UITableView()

    
    
    

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return cellCount // use a variable here so you can change it as you delete cells, else it will crash
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 118
    }
   
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CustomTv
        
   
        return cell
    }
    

    
  
    
    override func viewDidLoad() {
        super.viewDidLoad()
     
        [addBTN].forEach{
            $0.translatesAutoresizingMaskIntoConstraints = false
            view.addSubview($0)
        }
        tableview.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height * 0.8)
        addBTN.frame = CGRect(x: view.frame.width / 2, y: view.frame.height * 0.8, width: view.frame.width / 2  , height: view.frame.height / 5)
        addBTN.backgroundColor = .systemTeal
      
        view.addSubview(tableview)

 
        tableview.register(CustomTv.self, forCellReuseIdentifier: "cell")
        tableview.delegate = self
        tableview.dataSource = self
        pic.isHidden = true
        pic.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height )
        
      
        addBTN.addTarget(self, action: #selector(newCell), for: .touchDown)
        
        
        addBTN.setTitle("New cell", for: .normal)
    }
    @objc func newCell(){
        
        arr.append(6) /// not sure what the numbers mean though...
        tableview.beginUpdates()
        tableview.insertRows(at: [IndexPath(row: arr.count - 1, section: 0)], with: .automatic) /// animate the insertion
    tableview.endUpdates()

    }
    
}



protocol CustomTvCellDelegateadd: AnyObject {
    func addCeller(_ customTV: CustomTv)
}
class CustomTv: UITableViewCell { // it's good form to Pascal case your class name ;)
    
    weak var delegate: CustomTvCellDelegate? // make that delegate a property of your cell
    weak var delegatef: CustomTvCellDelegateadd?
    lazy var backView : UIView = {
        let view = UIView(frame: CGRect(x: 10, y: 6, width: self.frame.width  , height: 110))
        view.backgroundColor = .green
        
        return view
    }()



        lazy var pic : UIImageView = {
            let btn = UIImageView(frame: CGRect(x: 50-25, y: 6, width: 100 , height: 110))
            btn.backgroundColor = .systemPink
    
            return btn
    
        }()


    override func layoutSubviews() {
        backView.clipsToBounds = true
        backView.frame =  CGRect(x: 0, y: 6, width: bounds.maxX  , height: 110)
        
        // moved these calls here instead of on setSelected(_selected:animated:)
        addSubview(backView)
       

        backView.addSubview(pic)
        
        

        
 
      
    }
        @objc func addButtonTapped() {
            self.delegatef?.addCeller(self)
        }
}

标签: arraysswiftuitableviewappend

解决方案


那是因为您向名为 arr 的数组附加了一个值 (6),但是当 tableView 询问您返回的部分中的行数时cellCount,您从不费心增加,因此它将继续返回 5。

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return cellCount // use a variable here so you can change it as you delete cells, else it will crash
    }

因此,当tableview.insertRows执行时,tableView 期望行数增加数组中的元素数(在您的情况下,它期望行数为 6,但由于它的值不变, cellCount它只会得到 5,因此 tableView 不知道并抛出错误提及

更新后现有节中包含的行数 (5) 必须等于更新前该节中包含的行数 (5),加上或减去从该节中插入或删除的行数(插入 1 , 0 已删除)并加上或减去移入或移出该部分的行数(0 移入,0 移出)。”

所以要么改变

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return arr.count
    }

或使用

@objc func newCell(){
        cellCount += 1 // update the value
        arr.append(6)
        tableview.beginUpdates()
        tableview.insertRows(at: [IndexPath(row: arr.count - 1, section: 0)], with: .automatic) /// animate the insertion
    tableview.endUpdates()

}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return cellCount
}

哪个对你更有意义:)


推荐阅读