gpt4 book ai didi

ios - 1 个 View Controller 中的 2 个 tableViews - 错误不断变化

转载 作者:行者123 更新时间:2023-12-01 18:34:24 25 4
gpt4 key购买 nike

我在一个 View Controller 中有两个 TableView 。当单击第 1 组按钮时,它们被添加到 tableview 1,当单击第 2 组按钮时,它们应该被添加到 tableview 2。但无论我更改什么,我都会收到以下 3 个错误之一:

  • “无效更新”
  • "尝试将第 0 行插入第 0 节,
  • “无法使具有标识符的单元格出列”

  • 代码:
        @IBOutlet weak var tableView:  UITableView!
    @IBOutlet weak var tableView2: UITableView!


    var pizzas = [String]()
    var drinks = [String]()



    @IBAction func onAddTapped(sender:UIButton) {

    pizzaCount = pizzaCount + 1
    pizzaCounter.text = ("Antal pizza: ") + "\(pizzaCount)"

    func add(_ pizza: String) {
    pizzas.append(pizza)
    tableView.insertRows(at: [IndexPath(row: pizzas.count - 1, section: 0)], with: .fade)
    }
    add(sender.title(for: .normal)!)

    }
    @IBAction func onAddTappedDrinks(sender:UIButton) {

    func add(_ drink: String) {
    drinks.append(drink)
    tableView2.insertRows(at: [IndexPath(row: drinks.count - 1, section: 0)], with: .fade)
    }
    add(sender.title(for: .normal)!)

    }[![enter image description here][1]][1]
    }


    extension ViewController: UITableViewDataSource, UITableViewDelegate {

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {


    return pizzas.count

    }

    func tableView2(_ tableView2: UITableView, numberOfRowsInSection section: Int) -> Int {


    return drinks.count

    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    return tableView.dequeueReusableCell(withIdentifier: "item", for: indexPath)

    }



    func tableView2(_ tableView2: UITableView, cellForRowAt indexPath2: IndexPath) -> UITableViewCell {
    return self.tableView2.dequeueReusableCell(withIdentifier: "item2", for: indexPath2)

    }

    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    guard let cell = cell as? itemTableViewCell else
    { return }

    let pizza = pizzas[indexPath.row]
    cell.titleLabel.text = pizza
    cell.detailTextLabel?.text = ("hu")
    }

    func tableView2(_ tableView2: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath2: IndexPath) {
    guard let cell = cell as? item2TableViewCell else { return }

    let drink = drinks[indexPath2.row]
    cell.titleLabel.text = drink
    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)

    }


    func tableView2(_ tableView2: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath2: IndexPath) {
    guard editingStyle == .delete else { return }
    drinks.remove(at: indexPath2.row)
    //
    tableView2.deleteRows(at: [indexPath2], with: .automatic)
    }

    override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    tableView.dataSource = self
    tableView.delegate = self

    // tableView1.register(UITableViewCell.self, forCellReuseIdentifier: "CountryCell")

    tableView2.dataSource = self
    tableView2.delegate = self
    tableView2.register(UITableViewCell.self, forCellReuseIdentifier: "item2")

    }






    }

    最佳答案

    这不是tableView's UITableViewDataSourceUITableViewDelegate方法起作用。

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {


    func tableView2(_ tableView2: UITableView, numberOfRowsInSection section: Int) -> Int {
    numberOfRowsInSection 编写 2 种方法是不正确的。第二个不是 UITableViewDataSource方法,所以它永远不会被调用。
    您只需创建一个方法,然后检查 tableView 的实例。和 tableView2在里面,然后相应地编码..
    例如:
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if tableView == self.tableView {
    return pizzas.count
    } else {
    return drinks.count
    }
    }
    这适用于所有 UITableViewDataSource、UITableViewDelegate 方法。

    关于ios - 1 个 View Controller 中的 2 个 tableViews - 错误不断变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62639100/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com