【问题标题】:Strange issue with displaying cells in UITableView在 UITableView 中显示单元格的奇怪问题
【发布时间】:2017-10-13 16:55:34
【问题描述】:

我有 UITableView 的方法:

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
override func numberOfSections(in tableView: UITableView) -> Int
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell

所以,问题是 Xcode 一开始就运行

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

返回 1。 后来它运行 override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int 也返回我 1。

但是我不知道为什么在这两种方法之后它没有运行cellForRowAt 方法并且没有在我的 UITableView 上显示任何行。

我不知道那里发生了什么以及为什么会这样。 您是否遇到过这样的问题,您能帮我解决吗?

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if let fetchedResultsController = self.fetchedResultsController, let fetchedObjects = fetchedResultsController.fetchedObjects {
        if !searchController.isActive {
            print("numberOfRowsInSection fetchedObjects.count - \(fetchedObjects.count)")
            return fetchedObjects.count
        } else {
            if let results = filteredResult[searchController.searchBar.selectedScopeButtonIndex] {
            print("numberOfRowsInSection results.count - \(results.count)")
                return results.count
            }
        }
    }
    print("numberOfRowsInSection - 0")
    return 0
}

override func numberOfSections(in tableView: UITableView) -> Int {
    if let frc = fetchedResultsController, frc.fetchedObjects?.count != 0 {      
        print("numberOfSections - 1")                              
        return 1
    } else {
        print("numberOfSections - 0")            
        return 0
    }
}

【问题讨论】:

  • 检查这些方法中的行数和节数。其中任何一个都为零 (0) 或您的表格可能不可见(屏幕的可见区域)
  • @Krunal 是的。听起来很合乎逻辑。但是我检查了很多次!我没有得到 0。我只看到 UITableView 的标题,但行字段为空
  • 您是否通过在cellForRowAt 方法中添加打印语句来检查它是否运行,只是为了确定?
  • @Krunal 我在我的viewWillAppear 中输入了tableView.reloadData(),并在那里进行了调试,检查了(lldb) po tableView.numberOfSections 的值>> 1(lldb) po tableView.numberOfRows(inSection: 0) >> 1 所以,行必须在那里!
  • @Krunal 好的,我会这样做,但我刚刚注意到这个Serious application error. An exception was caught from the delegate of NSFetchedResultsController during a call to -controllerDidChangeContent:. Invalid update: invalid number of sections. The number of sections contained in the table view after the update (1) must be equal to the number of sections contained in the table view before the update (0), plus or minus the number of sections inserted or deleted (0 inserted, 0 deleted). with userInfo (null) 有用吗?

标签: ios iphone swift uitableview


【解决方案1】:

可能发生的情况是您的单元格的高度为0。发生这种情况时,根本不会调用 cellForRowAt,即使其他方法返回非零 section/row 计数。

我对为什么会发生这种情况的猜测是,您可能正在对单元格使用自动布局,但您的约束不允许单元格计算出自己的高度。您可能在不知不觉中使用了自动布局,因为在 iOS 11 上,它现在是默认设置。如果您使用故事板,您可以在属性检查器上设置单元原型的高度,而不是选中 automatic 框。或者,您可以使用tableView.rowHeight 或通过实现func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat 在代码中设置它

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-10
    • 2017-07-10
    相关资源
    最近更新 更多