我正在尝试制作表格,但我不断收到错误

问题描述 投票:0回答:1

我尝试了很多方法来修复这个错误。我对 IOS 编程比较陌生。

这是我不断收到的错误

(无效更新:无效的节数。更新后表视图中包含的节数(1)必须等于更新前表视图中包含的节数(0),加上或减去数字插入或删除的部分(0 插入,0 删除); 34, 0}; 数据源: >") 代码是做个表格这里就是代码如下

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) } }
    
                
表格视图能够管理按部分组织的数据。您可以有任意数量的部分,每个部分可以包含不同数量的项目。 (想象一本按字母表字母组织的百科全书。每个部分都是字母表中的一个字母,并且包含以该字母开头的文章。)
ios swift uitableview uikit
1个回答
0
投票
您还可以创建包含单个部分的表格视图。在这种情况下,它只是一个简单的项目列表。

正如 Paulw11 所建议的,您的表格视图看起来包含一个部分。因此,您应该重写 numberOfSections 函数以始终返回 1:

override func numberOfSections(in tableView: UITableView) -> Int { // #warning Incomplete implementation, return the number of sections return 1 }


© www.soinside.com 2019 - 2024. All rights reserved.