【发布时间】: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