代码之家  ›  专栏  ›  技术社区  ›  Mr. Man

UITableView单元格插入失败

  •  0
  • Mr. Man  · 技术社区  · 9 年前

    我正在尝试制作一个UITableView,它可以具有可扩展的标题视图。当您按下标题视图内的按钮时,将执行以下功能:

    func expandTheCell(_ sender: UIButton) {
        self.tableView.beginUpdates()
    
        if sender.isExpanded == false {
    
            postsArray.append("Hello")
            tableView.reloadData()
            print("Array Count: \(postsArray.count)")
            self.tableView.insertRows(at: [IndexPath.init(row: 0, section: sender.tag)], with: .fade)
    
        } else {
            print("test")
        }
    
        self.tableView.endUpdates()
    }
    

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {        
        return postsArray.count
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 3
    }
    

    当我尝试插入行时,出现以下错误:

    由于未捕获异常而终止应用程序 “NSInternalInconsistencyException”,原因:“无效更新:无效 第1节中的行数

    1 回复  |  直到 9 年前
        1
  •  0
  •   3stud1ant3    9 年前

    我认为问题是由于有相同的 数据源阵列 在您的案例中 postsArray postsArray公司 postsArray公司 section 0 , section 1 抱怨插入操作前后我的行数不一样,但 第0节 不要抱怨,因为它在中具有相同的行数和项数

    现在这个问题可以通过两种方式解决:

    postsArray公司

    postsArray1 对于第1节, postsArray2

    class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
            @IBOutlet weak var tableView: UITableView!
    
            override func viewDidLoad() {
                    super.viewDidLoad()
    
                    let addButton = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(buttonTapped(_:)))
                    self.navigationItem.rightBarButtonItem = addButton
            }
    
    
            var valuesFirstSection = ["value1", "value2", "value3"]
            var valuesSecondSection = ["value1Second", "value2Second", "value3Second"]
    
            //if you want to have the same dataSource array then use this
    
            //var sharedValues = ["value1Shared", "value2Shared", "value3Shared"] // shared dataSource array
    
    
            func numberOfSections(in tableView: UITableView) -> Int {
                    return 2
            }
            func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    
                    if section == 0 {
                            return valuesFirstSection.count
                    }else {
                            return valuesSecondSection.count
                    }
    //              //if you want to have the same dataSource array then
                    //use this
    
                    //return sharedValues.count;
            }
    
            func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
                    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
    
                    if indexPath.section == 0 {
                            cell.textLabel?.text = valuesFirstSection[indexPath.row]
    
    
                    }else {
    
                            cell.textLabel?.text = valuesSecondSection[indexPath.row]
    
                    }
                    return cell
    
                    //if you want to have the same dataSource array then
                    //use this
    
                    //cell.textLabel?.text = sharedValues[indexPath.row]
                    //return cell
    
            }
    
            func buttonTapped(_ sender: UIBarButtonItem) {
    
    
    
                    //if you want to have the same dataSource array then
                    //you need to insert the rows for other sections as well
    //                sharedValues.insert("NewValue0", at: 0)
    //                self.tableView.insertRows(
    //                        at: [IndexPath(row: 0, section: 0),
    //                             IndexPath(row: 0, section: 1)
    //                        ],
    //                        with: .automatic)
    
    
                    valuesFirstSection.insert("NewValue0", at: 0)
                    self.tableView.insertRows(
                            at: [IndexPath(row: 0, section: 0)
                            ],
                            with: .automatic)
    
    
            }
    
    
    
    }
    

    希望这有帮助。