IOS/Objective-C:没有结果时在表视图的背景视图中显示消息



当获取的结果控制器不返回此处的行时,我正在尝试显示未找到结果的消息。

为此,我将代码放在 numberOfRows 方法中。 但是,它正在引发异常。 这是我的代码:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (tableView == self.searchDisplayController.searchResultsTableView) {
return [searchResults count];
} else {
id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections]objectAtIndex:section];
int numRows =[sectionInfo numberOfObjects];
if (numRows>=1) {
return [sectionInfo numberOfObjects];
}
else {
UILabel *messageLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
messageLabel.text = @"No comments yet.";
messageLabel.textColor = [UIColor grayColor];
messageLabel.numberOfLines = 0;

self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
return 0;
}
}

我应该把这段代码放在其他地方吗?或者它可能有什么问题会导致它引发异常。

提前感谢您的任何建议。

使用节数而不是节中的行数。 像这样的东西。

if self.yourArray.count > 0 {
return 1
} else {
//Your Empty Message
return 0
}

最新更新