3.5 C
New York
Saturday, February 22, 2025

ios – I’m making an attempt to make a desk however I hold getting errors


I attempted some ways to repair this error. I comparatively new to IOS programming.

That is the error I hold getting

(Invalid replace: invalid variety of sections. The variety of sections contained within the desk view after the replace (1) should be equal to the variety of sections contained within the desk view earlier than the replace (0), plus or minus the variety of sections inserted or deleted (0 inserted, 0 deleted). Desk view: ; backgroundColor = ; layer = ; contentOffset: {0, -59}; contentSize: {393, 44}; adjustedContentInset: {59, 0, 34, 0}; dataSource: >”)

The code is to make a desk right here is the code under

import UIKit

class SceneDelegate: UIResponder, UIWindowSceneDelegate {

    var window: UIWindow?
    

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, choices connectionOptions: UIScene.ConnectionOptions) {
        // Use this methodology to optionally configure and fasten the UIWindow `window` to the supplied UIWindowScene `scene`.
        // If utilizing a storyboard, the `window` property will robotically be initialized and hooked up to the scene.
        // This delegate doesn't suggest the connecting scene or session are new (see `utility:configurationForConnectingSceneSession` as an alternative).
        guard let _ = (scene as? UIWindowScene) else { return }
        
        let itemStore = ItemStore()
        
        let itemController = window!.rootViewController as! ItemsViewController
        
        itemController.itemStore = itemStore
    }

import UIKit

class Merchandise: Equatable {
    var identify: String
    var valueInDollars: Int
    var serialNumber: String?
    var dateCreated: Date
    
    init(identify: String, serialNumber: String?, valueInDollars: Int) {
        self.identify = identify
        self.valueInDollars = valueInDollars
        self.serialNumber = serialNumber
        self.dateCreated = Date()
    }
    
    
    comfort 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.parts(separatedBy: "-").first!
            
            self.init(identify: randomName, serialNumber: randomSerialNumber, valueInDollars: randomValue)
            
        } else {
            self.init(identify: "", serialNumber: nil, valueInDollars: 0)
        }
    }
    
    static func ==(lhs: Merchandise, rhs: Merchandise) -> Bool {
        return lhs.identify == rhs.identify && lhs.serialNumber == rhs.serialNumber && lhs.valueInDollars == rhs.valueInDollars && lhs.dateCreated == rhs.dateCreated
    }
    
}

class ItemStore {
    
    var allItems = [Item]()
    @discardableResult func createItem() -> Merchandise {
        let newItem = Merchandise(random: true)
        
        allItems.append(newItem)
        
        return newItem
    }
    
    func removeItem(_ merchandise: Merchandise) {
        if let index = allItems.firstIndex(of: merchandise) {
            allItems.take away(at: index)
        }
    }
    
    func moveItem (from fromIndex: Int, to toIndex: Int) {
        if fromIndex == toIndex {
            return
        }
        
        let movedItem = allItems[fromIndex]
        
        allItems.take away(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: .regular)
            
            setEditing(false, animated: true)
        } else {
            sender.setTitle("Performed", for: .regular)
            
            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, part: 0)
            
            tableView.insertRows(at: [indexPath], with: .automated)
        }
        
    }
    
    
    
    override func viewDidLoad() {
        tremendous.viewDidLoad()
    }


    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the variety of sections
        return itemStore.allItems.rely
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection part: Int) -> Int {
        return itemStore.allItems.rely
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        // #warning Incomplete implementation, return the variety of rows
        let cell = tableView.dequeueReusableCell(withIdentifier: "UITableViewCell", for: indexPath)
        
        let merchandise = itemStore.allItems[indexPath.row]
        
        cell.textLabel?.textual content = merchandise.identify
        cell.detailTextLabel?.textual content = "(merchandise.valueInDollars)"
        
        return cell
    }
    
    override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
        if editingStyle == .delete {
            let merchandise = itemStore.allItems[indexPath.row]
            itemStore.removeItem(merchandise)
            tableView.deleteRows(at: [indexPath], with: .automated)
        }
    }
    
    override func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
        itemStore.moveItem(from: sourceIndexPath.row, to: destinationIndexPath.row)
    }

}

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles