gpt4 book ai didi

ios - (Swift) TableView Controller 。未触发 cellForRowAt

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

问题:
tableView Controller 未在表中显示 categoryArray 项目。

诊断:
- 第一个 tableView numberOfSections 方法被触发并返回 1 (print(categoryArray.count)
- 2nd tableView cellForRowAt 方法永远不会启动(打印语句不起作用)
- TableView Controller 不需要委托(delegate)。但是,我尝试添加 tableView.delegate = self
- x-code 重启没有帮助

问题:
为什么第二个 tableView 方法不起作用以及如何解决这个问题?

import UIKit
import CoreData

class CategoryViewController: UITableViewController {

var categoryArray = [Category]()
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext

override func viewDidLoad() {
super.viewDidLoad()
loadCategories()
}

// MARK: - TableView DataSource Methods

override func numberOfSections(in tableView: UITableView) -> Int {
return categoryArray.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
print("this should print")
let cell = tableView.dequeueReusableCell(withIdentifier: "CategoryCell", for: indexPath) as! CategoryCell

let category = categoryArray[indexPath.row]
cell.categoryNameTextField.text = category.name

return cell
}

最佳答案

  • 而不是 numberOfSections实现numberOfRows
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return categoryArray.count
    }
  • viewDidLoad 结尾重新加载表格 View
    override func viewDidLoad() {
    super.viewDidLoad()
    loadCategories()
    tableView.reloadData()
    }

    如果 loadCategories()加载数据时包含异步重新加载 TableView 的内容。
  • 关于ios - (Swift) TableView Controller 。未触发 cellForRowAt,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61294578/

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