【问题标题】:Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (1) must be equal to更新无效:第 0 部分中的行数无效。更新 (1) 后现有部分中包含的行数必须等于
【发布时间】:2022-11-22 01:51:54
【问题描述】:

我正在尝试使用滑动删除功能来删除 tableview 单元格。它工作正常,除非我只剩下 1 个单元格。如果我删除最后一个单元格,我会收到名义上的错误消息。这里出了什么问题?这似乎是一个内部逻辑错误,但我在删除该行之前从我的数组中删除了该项目,所以该行不应该反映这一点吗?提前致谢!

这是一些设置:

private var resultArray: [SavedResult] = []

private var decodedData = [SavedResult]()


@IBOutlet weak var resultsTableView: UITableView!

var resultLabel = "none"

override func viewDidLoad() {
    super.viewDidLoad()
    
    self.resultsTableView.delegate = self
    resultsTableView.dataSource = self
    resultsTableView.rowHeight = 80.0
    loadCellName()
    
}

func loadCellName() {
    
    resultArray = []
    
    
    if let data = try? Data(contentsOf: filePath!) {
     
    let decoder = PropertyListDecoder()
    do {
        decodedData = try decoder.decode([SavedResult].self, from: data)
    } catch {
        print("There was an error loading data: \(error)")
    }
    }
    resultArray = decodedData
    self.resultsTableView.reloadData()
}

这是我所有的 tableview/data source/swipe 东西:

  extension SavedViewController: UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        if resultArray.count == 0 {
            
            return 1
        } else {
        return resultArray.count
        }
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = resultsTableView.dequeueReusableCell(withIdentifier: "ResultCell", for: indexPath) as! SwipeTableViewCell
        
        cell.delegate = self
        
        if resultArray.count == 0 {

            cell.textLabel?.text = "No Saved Results Yet."
            cell.textLabel?.attributedText = NSAttributedString(string: (cell.textLabel?.text)!, attributes: [NSAttributedString.Key.font : UIFont(name: "Caveat", size: 30.0)])
            cell.backgroundColor = blueCell
            cell.accessoryType = .none
            cell.isUserInteractionEnabled = false
            return cell
        } else {
            
            cell.textLabel?.text = resultArray[indexPath.row].name
            
            cell.textLabel?.attributedText = NSAttributedString(string: (cell.textLabel?.text)!, attributes: [NSAttributedString.Key.font : UIFont(name: "Caveat", size: 30.0)])
            
            
            switch indexPath.row {
            case 0, 3, 6, 9, 12, 15, 18, 21, 24, 27:
                cell.backgroundColor = blueCell
            case 1, 4, 7, 10, 13, 16, 19, 22, 25:
                cell.backgroundColor = yellowCell
                case 2, 5, 8, 11, 14, 17, 20, 23:
                cell.backgroundColor = purpleCell
            default:
                fatalError("cell background colour setting failed.")
            }
            
            
            return cell
        }
        
    }

}

extension SavedViewController: UITableViewDelegate {
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        resultLabel = resultArray[indexPath.row].resultMessage
        resultsTableView.deselectRow(at: indexPath, animated: true)
        self.performSegue(withIdentifier: "savedExamSeg", sender: self)
}
}
   
extension SavedViewController: SwipeTableViewCellDelegate {
    
   
    func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath, for orientation: SwipeActionsOrientation) -> [SwipeAction]? {
        
        guard orientation == .right else { return nil }
        let deleteAction = SwipeAction(style: .destructive, title: "Delete") { action, indexPath in
                
                self.resultArray.remove(at: indexPath.row)

                let encoder = PropertyListEncoder()
                do {
                    
                    let data = try encoder.encode(self.resultArray)
                    try data.write(to: self.filePath!)
                } catch {
                    
                    print("There was an error saving data: \(error)")
                }
            
            }
        
        
        deleteAction.image = UIImage(named: "deleteIcon")
 
        return [deleteAction]
    }
    
    func tableView(_ tableView: UITableView, editActionsOptionsForRowAt indexPath: IndexPath, for orientation: SwipeActionsOrientation) -> SwipeOptions {
        
        var options = SwipeTableOptions()
        options.expansionStyle = .destructive
        return options
    }

【问题讨论】:

    标签: swift xcode delegates tableview swipe


    【解决方案1】:

    您必须告诉表视图删除该行

    self.resultArray.remove(at: indexPath.row)
    tableView.deleteRows(at: [indexPath], with: .fade)
    

    【讨论】:

    • 我之前试过这个(现在又试了一次),但它崩溃了,出现以下错误: Thread 1: "attempt to delete row 4 from section 0 which only contains 4 rows before the update" 。一般来说,如果没有这条线,当我滑动时,tableview 实际上是在删除行。当我到达表格视图的最后一行时,它只会失败并崩溃
    猜你喜欢
    • 1970-01-01
    • 2016-10-31
    • 2019-07-23
    • 1970-01-01
    • 1970-01-01
    • 2019-03-11
    • 2014-10-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多