1

My tableView worked fine, until I added sections yesterday and all day I keep getting "Invalid update: invalid number of rows in section 1."

I tap a button and its supposed to add it self to the tableview, but Ive done something wrong with the sections, but can't find the mistake. and the solutions on here don't seem to work.

any thoughts?

enter image description here

import UIKit

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
    
    
    
    @IBOutlet weak var tableView:  UITableView!
    
    let section = ["Dagrofa", "Inco"]
    var pizzas = [String]()
    var drinks = [String]()
    
    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        
        return self.section[section]
    }
    func numberOfSections(in tableView: UITableView) -> Int {
        return section.count
        
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        
        return pizzas.count
    }
    
    @IBAction func onAddTapped(sender:UIButton) {
        
        func add(_ pizza: String) {
            pizzas.append(sender.title(for: .normal) ?? "")
            tableView.insertRows(at: [IndexPath(row: pizzas.count - 1, section: 0)], with: .fade)
            tableView.reloadData()
        }
        add(sender.title(for: .normal)!)
        
    }
    
    @IBAction func onAddTapped1(sender:UIButton) {
        
        func add(_ pizzza: String) {
            pizzas.append(sender.title(for: .normal) ?? "")
            tableView.insertRows(at: [IndexPath(row: pizzas.count - 1, section: 1)], with: .fade)
            //            tableView.reloadData()
        }
        add(sender.title(for: .normal)!)
        
        
        
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        return tableView.dequeueReusableCell(withIdentifier: "listCell", for: indexPath)
        
    }
    
    
    
    
    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        
        let pizza = pizzas[indexPath.row]
        cell.textLabel?.text = ("1 x ") + pizza
        cell.detailTextLabel?.text = ("hu")
        
        
    }
    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
        guard editingStyle == .delete else { return }
        pizzas.remove(at: indexPath.row)
        
        tableView.deleteRows(at: [indexPath], with: .automatic)
        
    }
    
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        tableView.dataSource = self
        tableView.delegate = self
        
  
        
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "listCell")
        
    }
    
    
    
    
    
    
}
  • Does this answer your question? ['Invalid update: invalid number of rows in section 0](https://stackoverflow.com/questions/21870680/invalid-update-invalid-number-of-rows-in-section-0) – pkamb Jul 16 '20 at 20:40
  • I read that link earlier, but its about deletingRows? My app crashes when I tap the button to add to the tableview. – Sian Ricardo Jul 16 '20 at 20:43
  • 1
    In section 1 you have to add the item to `drinks`. And in `numberOfRowsInSection` you have to consider also the different sections. You can avoid those errors if you use a more suitable data source like a custom struct rather than multiple arrays. – vadian Jul 16 '20 at 20:54
  • 2
    In `numberOfRowsInSection` you are always returning `pizzas.count`. This works only when both arrays have the same number of elements (eg 0). As soon as you add something to the pizza array, you are also saying there is 1 object in the drinks array and this is unexpected. You need to change this function to return a result relevant to the section that is passed in. – Paulw11 Jul 16 '20 at 20:57
  • @Paulw11 Ay of course! I didn't think of that! Thanks! – Sian Ricardo Jul 16 '20 at 21:23
  • @vadian, I tried that too, but I think that's beyond my skill level. Do you know of any sites/links where I could find an example of custom structs that works with tableview rows added by taps? I can only find tutorials on tableviews that displays the data already in in the structs/ rows (but not added by taps) But thanks for the explanation! – Sian Ricardo Jul 16 '20 at 21:27
  • I just wrote an answer to a similar question a few days ago: https://stackoverflow.com/questions/62909423/swift-display-specific-data-for-each-tableview-section/62910085#62910085 – vadian Jul 16 '20 at 21:35

0 Answers0