2017-10-13 24 views
0

展示細胞奇怪的問題我的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

什麼可能發生的是,您的細胞有高度0。發生這種情況時,即使其他方法返回的計數值不爲零,也不會調用cellForRowAt

我的猜測是,爲什麼會發生這種情況,您可能會爲您的單元格使用自動佈局,但是您的約束不允許單元格計算出它自己的高度。您可能會在不知不覺中使用自動佈局,因爲在iOS 11上它現在是默認設置。如果您正在使用故事板,則可以在屬性檢查器中設置單元格原型的高度,而不是檢查automatic框。或者,您可以使用代碼tableView.rowHeight或實施func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat