iOS / Objective-C:无结果时在tableview的背景视图中显示消息

时间:2018-09-04 01:39:55

标签: ios objective-c tableview

当获取的结果控制器未返回任何行as here时,我试图显示一条未找到结果的消息。

为此,我将代码放入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;
        }
}

我应该将此代码放在其他地方吗?否则可能会导致抛出异常的问题是什么。

预先感谢您的任何建议。

1 个答案:

答案 0 :(得分:-1)

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

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