ios - UITableViewCell 被空数据源数组卡住

标签 ios swift xcode xib nib

我向服务器发出请求以获取数据,然后将其数据设置到我的数组中以处理 TableView。

尽管服务器响应是一个数据源数组也是一个,但一个单元格或几个单元格被卡住时有时会发生问题,但单元格无论如何都会堆叠。在我关闭之前仍然在屏幕上。 tableView contentOffset 也发生了变化:

enter image description here

The screenshot shows froze tableViewCell. But dataSource array is an empty, numberOfRowsInSection in section also equal to 0.

一些细节:

  • tableView.reloadData() 从主线程调用;
  • ViewController 位于 TabBarViewController 中;
  • 我在 Retain 周期检查我的模型对象类,似乎一切正常;
  • 我使用 xib 作为单元格。该单元格具有自定义内容 View 。 Cell 类中的所有 IBOutlets 都有弱引用。

请帮助我理解我哪里错了

编辑:

extension ViewController: UITableViewDataSource {

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        guard let numberOfRowsInSection = items?.count else {
            messageLabel?.text = messageText
            return 0
        }

        return numberOfRowsInSection
    }

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

        guard let cell = tableView.dequeueReusableCell(withIdentifier: "Identifier", for: indexPath) as? CustomTableViewCell else { return UITableViewCell() }

        // Cell settings

        return cell

    }
}

最佳答案

我的猜测是你缺少数据源方法

optional func numberOfSections(in tableView: UITableView) -> Int

https://developer.apple.com/documentation/uikit/uitableviewdatasource/1614860-numberofsections

The number of sections in tableView. The default value is 1.

当您的数组中有 0 个项目时,这应该会破坏您的实现。根据您的结构,它可能看起来类似于

override func numberOfSections(in tableView: UITableView) -> Int {
    guard let arr = items else { return 0 }
    return arr.count == 0 ? 0 : 1
}

关于ios - UITableViewCell 被空数据源数组卡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53316290/

相关文章:

ios - 如何为 IOS 7 实现宏?

iphone - 按键拍照,无摄像头显示

ios - SWIFT:切换 rootviewcontroller

ios - 'RLMException',原因 : 'Realm accessed from incorrect thread.'

ios - Xcode 12 找不到任何匹配的 iOS App Development 配置文件

ios - iPhone 7 不显示网页 View

swift 4.0 : How do i move all the NSArray values to NSDictionary dynamically?

ios - 使用 CLLocation 和 Google Map 获取楼层信息的问题

swift - iOS Swift 如何在本地json文件中写入数据?

ios - 仪器 > 分配不显示应用程序类/对象