IOS / Objective-C:没有结果时在tableview的后台视图中显示消息

问题描述 投票:0回答:1

当尝试获取的结果控制器没有返回任何行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;
        }
}

我应该将此代码放在别处吗?或者可能导致它抛出异常的错误。

在此先感谢您的任何建议。

ios objective-c tableview
1个回答
-1
投票

使用部分的数量而不是部分中的行数。像这样的东西。

if self.yourArray.count > 0 {
    return 1
} else {
   //Your Empty Message
   return 0
}
© www.soinside.com 2019 - 2024. All rights reserved.