I tried many ways to fix this error. I relatively new to IOS programming.
This is the error I keep getting
(Invalid update: invalid number of sections. The number of sections contained in the table view after the update (1) must be equal to the number of sections contained in the table view before the update (0), plus or minus the number of sections inserted or deleted (0 inserted, 0 deleted). Table view: <UITableView: 0x106827800; frame = (0 0; 393 852); clipsToBounds = YES; autoresize = W+H; gestureRecognizers = <NSArray: 0x600000c6f030>; backgroundColor = <UIDynamicSystemColor: 0x60000175a8c0; name = tableBackgroundColor>; layer = <CALayer: 0x600000239820>; contentOffset: {0, -59}; contentSize: {393, 44}; adjustedContentInset: {59, 0, 34, 0}; dataSource: <tables_final.ItemsViewController: 0x105b06220>>")
The code is to make a table here is the code below
import UIKit
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
// Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
// If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
// This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
guard let _ = (scene as? UIWindowScene) else { return }
let itemStore = ItemStore()
let itemController = window!.rootViewController as! ItemsViewController
itemController.itemStore = itemStore
}
import UIKit
class Item: Equatable {
var name: String
var valueInDollars: Int
var serialNumber: String?
var dateCreated: Date
init(name: String, serialNumber: String?, valueInDollars: Int) {
self.name = name
self.valueInDollars = valueInDollars
self.serialNumber = serialNumber
self.dateCreated = Date()
}
convenience init(random: Bool = false) {
if random {
let adjectives = ["Fluffy", "Rusty", "Shiny"]
let nouns = ["Bear", "Spork", "Mac"]
let randomAdjectives = adjectives.randomElement()!
let randomNoun = nouns.randomElement()!
let randomName = "\(randomAdjectives) \(randomNoun)"
let randomValue = Int.random(in: 0..<100)
let randomSerialNumber = UUID().uuidString.components(separatedBy: "-").first!
self.init(name: randomName, serialNumber: randomSerialNumber, valueInDollars: randomValue)
} else {
self.init(name: "", serialNumber: nil, valueInDollars: 0)
}
}
static func ==(lhs: Item, rhs: Item) -> Bool {
return lhs.name == rhs.name && lhs.serialNumber == rhs.serialNumber && lhs.valueInDollars == rhs.valueInDollars && lhs.dateCreated == rhs.dateCreated
}
}
class ItemStore {
var allItems = [Item]()
@discardableResult func createItem() -> Item {
let newItem = Item(random: true)
allItems.append(newItem)
return newItem
}
func removeItem(_ item: Item) {
if let index = allItems.firstIndex(of: item) {
allItems.remove(at: index)
}
}
func moveItem (from fromIndex: Int, to toIndex: Int) {
if fromIndex == toIndex {
return
}
let movedItem = allItems[fromIndex]
allItems.remove(at: fromIndex)
allItems.insert(movedItem, at: toIndex)
}
/*
init() {
for _ in 0..<5 {
createItem()
}
}
*/
}
class ItemsViewController: UITableViewController {
var itemStore: ItemStore!
@IBAction func toggleEditModel(_ sender: UIButton) {
if isEditing {
sender.setTitle("Edit", for: .normal)
setEditing(false, animated: true)
} else {
sender.setTitle("Done", for: .normal)
setEditing(true, animated: true)
}
}
@IBAction func addNewItem(_ sender: UIButton) {
let newItem = itemStore.createItem()
if let index = itemStore.allItems.firstIndex(of: newItem) {
let indexPath = IndexPath(row: index, section: 0)
tableView.insertRows(at: [indexPath], with: .automatic)
}
}
override func viewDidLoad() {
super.viewDidLoad()
}
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return itemStore.allItems.count
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return itemStore.allItems.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// #warning Incomplete implementation, return the number of rows
let cell = tableView.dequeueReusableCell(withIdentifier: "UITableViewCell", for: indexPath)
let item = itemStore.allItems[indexPath.row]
cell.textLabel?.text = item.name
cell.detailTextLabel?.text = "\(item.valueInDollars)"
return cell
}
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
let item = itemStore.allItems[indexPath.row]
itemStore.removeItem(item)
tableView.deleteRows(at: [indexPath], with: .automatic)
}
}
override func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
itemStore.moveItem(from: sourceIndexPath.row, to: destinationIndexPath.row)
}
}
Table views are able to manage data organized in sections. You can have any number of sections, each of which can contain a different number of items. (Imagine an encyclopedia organized by letter of the alphabet. Each section would be a letter of the alphabet, and would contain the articles beginning with that letter.)
You can also create a table view with a single section. In that case it's just a flat list of items.
As Paulw11 suggests, it looks like your table view contains a single section. Thus you should rewrite your numberOfSections function to always return 1:
override func numberOfSections(in tableView: UITableView) -> Int {
// This is a single-section table view.
return 1
}