ios - 为什么我的 NO RESULTS 单元格没有显示在我的 TableView Controller 中?

标签 ios uitableview

我有一个具有此 cellForRowAtIndexPath 方法的应用程序:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    TableViewCell *cell = nil;

    //IF NO STORES FOUND
    if ([self.annotationsToSort count] == 0) {
        NSLog(@"No results");
        cell.nameLabel.text = @"No results";
    }

    // Check to see whether the normal table or search results table is being displayed    
    //IF ITS A SEARCH TABLE....>>>>>>>>>
    if (tableView == self.searchDisplayController.searchResultsTableView) {
        static NSString *CellIdentifier = @"HolidayCell";
        if (cell == nil) {
            cell = [[TableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
        }
        cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];

        Location *location = [filteredResultsArray objectAtIndex:indexPath.row];
        cell.nameLabel.text = location.nombrePublico;

    } else {

    // IF ITS A REGULAR TABLE...>>>>>>>>>>
        static NSString *CellIdentifier = @"HolidayCell";
        if (cell == nil) {
            cell = [[TableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
            //cell.accessoryType=UITableViewCellAccessoryDetailDisclosureButton;
        }
        cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];


        //USING SORTED ARRAY INSTEAD OF CDfetched ARRAY
        MyLocation *myLocation = [self.annotationsToSort objectAtIndex:indexPath.row];

        cell.nameLabel.text = myLocation.name;
        cell.dateLabel.text =  myLocation.address;
        cell.distancia.text = distance;
        cell.phoneLabel.text = myLocation.telefono;
        cell.openLabel.text = myLocation.estado;
        cell.driveThruLabel.text = myLocation.driveThru;

    }
    return cell;
}

但即使数据中没有结果,它也会返回没有我的无结果文本的空单元格。行方法是:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    // Return the number of rows in the section.
    if (tableView == self.searchDisplayController.searchResultsTableView) {
        return [filteredResultsArray count];
    } else {
        //return [self.farSiman count]; WAS CRASHING
        return [self.annotationsToSort count];
    }

}

我已经尝试将 NO STORES FOUND 代码块放入 NORMAL STATE TABLE if/else 中,但没有成功。

最佳答案

如果 [self.annotationsToSort count] 为零,则您的 numberOfRowsInSection 方法 返回零,因此根本不会调用 cellForRowAtIndexPath,并且返回一个空的 显示 TableView 。

如果你想显示一个特殊的单元格“没有结果”,那么你必须修改 numberOfRowsInSection 以便在这种情况下返回 1。 例如,(未经测试,只是为了给您提供思路):

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if ([self.annotationsToSort count] == 0) {
        return 1;
    } else if (tableView == self.searchDisplayController.searchResultsTableView) {
        return [filteredResultsArray count];
    } else {
        return [self.annotationsToSort count];
    }
}

您的 cellForRowAtIndexPath 方法也有一些错误。例如, 在 [self.annotationsToSort count] == 0 的情况下没有分配单元格。 它应该看起来更像这样(同样未经测试):

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"HolidayCell";
    cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[TableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }

    //IF NO STORES FOUND
    if ([self.annotationsToSort count] == 0) {
        NSLog(@"No results");
        cell.nameLabel.text = @"No results";
    } else if (tableView == self.searchDisplayController.searchResultsTableView) {
        Location *location = [filteredResultsArray objectAtIndex:indexPath.row];
        cell.nameLabel.text = location.nombrePublico;
    } else {
        MyLocation *myLocation = [self.annotationsToSort objectAtIndex:indexPath.row];

        cell.nameLabel.text = myLocation.name;
        cell.dateLabel.text =  myLocation.address;
        cell.distancia.text = distance;
        cell.phoneLabel.text = myLocation.telefono;
        cell.openLabel.text = myLocation.estado;
        cell.driveThruLabel.text = myLocation.driveThru;
    }
    return cell;
}

关于ios - 为什么我的 NO RESULTS 单元格没有显示在我的 TableView Controller 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18724877/

相关文章:

ios - 使用 scrollViewDidScroll 移动 ChildViewController 的 subview

ios - 如何使用滚动动画关注 UITableview 中的最后一个单元格?

swift - 使用 UISearchResultsUpdating 过滤表格 View

ios - 如何使用 Delegate 方法知道按钮在哪个单元格中被点击。 swift

ios - SpriteKit View 不在 ViewController 的 viewDidLoad 中呈现场景

ios - 只检索存储在UserDefaults中的键和值

ios - 侧边菜单 revealcontroller 问题与 UITableViewController

ios - 在 Apple 的 SpeakHere 示例中设置 kAudioSessionMode 会导致音量大幅下降。任何解决方法?

iphone - UITableView 中 2 种不同类型的自定义 UITableViewCells

ios - 如何使用 Xcode 机器人在模拟器上运行 UIAutomation - 奇怪的行为