【问题标题】:UITableView cellForRowAt method is not getting executedUITableView cellForRowAt 方法没有被执行
【发布时间】:2022-11-12 21:11:21
【问题描述】:
extension ArticlesViewController {
    func setup() {
        self.navigationController?.navigationBar.prefersLargeTitles = true
    
    newtworkManager?.getNews { [weak self] (results) in
        switch results {
        case .success(let data):
            self?.articleListVM = ArticleListViewModel(articles: data.article!)
            // For testing
            print(self?.articleListVM.articles as Any)
            
            DispatchQueue.main.async {
                self?.tableView.reloadData()
            }
        case .failure(let error):
            print(error.localizedDescription)
        }
    }
}

现在,在调试时,我正在成功接收数据并将其打印出来。但是,我意识到 cellForRowAt 函数没有被执行,这导致数据没有显示在表格上。我看不出任何问题,但运行时当然不同意。

extension ArticlesViewController {
override func numberOfSections(in tableView: UITableView) -> Int {
    return self.articleListVM == nil ? 0 : self.articleListVM.numberOfSections
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return self.articleListVM.numberOfRowsInSection(section)
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    guard let cell = tableView.dequeueReusableCell(withIdentifier: "ArticleTableViewCell", for: indexPath) as? ArticleTableViewCell else {
        fatalError("ArticleTableViewCell not found")
    }
    
    let articleVM = self.articleListVM.articleAtIndex(indexPath.row)
    cell.titleLabel.text = articleVM.title
    cell.abstractLabel.text = articleVM.abstract
    return cell
}

}

为什么你认为这个方法没有被触发?请注意,故事板上的 UITableView 和 UITableViewCell 分别连接到我的代码。我看不出它没有加载数据的原因。

【问题讨论】:

  • 您是否在 viewDidLoad() 中设置了委托? tableView.delegate = self tableView.dataSource = self
  • @NomanUmar 是的,已经这样做了,但根本无法工作。
  • 如果 numberOfRowsInSection > 0 & numberOfSections >= 1,我会仔细检查你的约束。作为尝试,您可以在设置中添加 tableView.rowHeight = 50 并查看它是否被调用
  • @AhmedAlFailakawi 如果问题出在返回 numberOfRowsInSection 中的 0 个原始文件或返回 numberOfSections 中的 0 个部分,并且您发现您的错误并且问题已解决 - 请关闭您的问题。

标签: ios swift uitableview uikit


【解决方案1】:

将 ArticlesViewController 确认为 UITableViewDelegate 和 UITableViewDataSource 协议并删除函数的覆盖。 例子:

extension ArticlesViewController: UITableViewDelegate, UITableViewDataSource {
    func numberOfSections(in tableView: UITableView) -> Int {
        return self.articleListVM == nil ? 0 : self.articleListVM.numberOfSections
    }

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

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        ....
    }
}

还要确保您已通过故事板/代码连接了您的表格视图。

tableView.dataSource = self
tableView.delegate = self

【讨论】:

  • ArticlesViewController 是一个 UITableViewController,它已经符合 UITableViewDelegate、UITableViewDataSource 两个协议。在进行更多调试时,我注意到 cellForRowAt 根本没有被执行。
  • numberOfRowsInSection 函数是否正在执行..?如果是,返回的数字是多少..?
  • numberOfRowsInSection = 0
  • 糟糕......这就是为什么单元格没有加载的原因......添加一些硬编码值,如 return 5 & test,以便单元格可以加载。并且,调试这条语句“return self.articleListVM == nil ? 0 : self.articleListVM.numberOfSections”。这应该可以解决您的问题。 :)
  • 你看,我创建了一个类似的项目,它完全一样,代码也是一样的。主要区别在于这个使用的是不同的网络库。在调试其他项目时,numberOfSections = 0 没有问题,它完美地显示了所有数据单元格。为什么会出现这个问题?
【解决方案2】:

我的主要问题是全局变量 newtworkManager 正如您在代码中看到的那样:

newtworkManager?.getNews {...}

解决方案是我删除了这个全局变量并将其替换为以下内容:

NetworkManager().getNews { ...}

之后,cellForRowAt 方法工作正常,数据单元格显示在 UITableView 上。

【讨论】:

    【解决方案3】:

    您如何以编程方式或通过 Storyboard 布置视图?

    如果通过情节提要完成:

    1. 确保正确连接 IBOutlet(检查拼写错误等), 将委托和数据源分配给表格视图并符合 协议。

      如果以编程方式完成:

      1. 确保在之前设置了数据的委托和数据源 打电话给tableView.reloadData()

        tableView.dataSource = self
        tableView.delegate = self
        

    【讨论】:

    • 我通过情节提要创建了它,看起来还不错。数据源和委托已正确连接到 IBOutlets。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-26
    相关资源
    最近更新 更多