在UITableView的

问题描述:

展示细胞奇怪的问题我的UITableView与方法:在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方法,并没有显示任何r在我的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

检查这些方法中行数和段数。任何人将是零(0)或您的表可能不可见(屏幕的可见区域) – Krunal

+0

@ Krunal是的。这听起来很合理。但我检查了很多次!我没有得到0.我只看到了我的UITableView的头部,但行字段为空 –

+0

您是否已通过在'cellForRowAt'方法中添加打印语句进行检查,如果它运行或不运行,只是为了确定? – 3stud1ant3

什么可能发生的是,您的细胞有高度0。发生这种情况时,即使其他方法返回的计数值不为零,也不会调用cellForRowAt

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