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?
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")
}
}