UISearchDisplayController 在没有结果时显示自定义 UITableViewCell

UISearchDisplayController display custom UITableViewCell when No Results

我有一个 UITableView,其中包含一些自定义 UITableViewCells 和一个 UISearchDisplayController 连接到它。如果在搜索内容时没有结果,我如何显示另一个自定义 Table 查看单元格?

搜索和 table 查看功能如下所示:

- (void)searchTableList:(UISearchBar*)searchBar {
    NSString *searchString = searchBar.text;

    for (NSArray *section in _celebs)
        for (NSDictionary *row in section) {
            if ([[row[@"name"] lowercaseString] rangeOfString:[searchString lowercaseString]].location!=NSNotFound)
            [filteredContentList addObject:row];
    }

}

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
    isSearching = YES;
}

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {

    [filteredContentList removeAllObjects];

    if (searchText.length) {
        isSearching = YES;
        [self searchTableList:searchBar];
    } else isSearching = NO;
}

- (void)searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller{
}

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
    isSearching = NO;
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    [self searchTableList:searchBar];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return [tableView isEqual:self.searchDisplayController.searchResultsTableView]?1:_objects.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [tableView isEqual:self.searchDisplayController.searchResultsTableView]?filteredContentList.count:[_objects[section] count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return [tableView isEqual:self.searchDisplayController.searchResultsTableView]?0:25;
}

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForHeaderInSection:(NSInteger)section {
    return [self tableView:tableView heightForHeaderInSection:section];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 55;
}

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [self tableView:tableView heightForRowAtIndexPath:indexPath];
}

- (NSDictionary *)getObjectFromIndexPath:(NSIndexPath*)indexPath tableView:(UITableView*)tableView {
    BOOL search = [tableView isEqual:self.searchDisplayController.searchResultsTableView];
    if (search)
        return indexPath.row<filteredContentList.count?filteredContentList[indexPath.row]:nil;
    else return indexPath.section<_objects.count?
        (indexPath.row<[_objects[indexPath.section] count]?_objects[indexPath.section][indexPath.row]:nil)
        :nil;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    BOOL search = [tableView isEqual:self.searchDisplayController.searchResultsTableView];

    FirstTableViewCell *cell;
    NoResultsTableViewCell *cell1;

    if (tableView == self.searchDisplayController.searchResultsTableView) {
        cell = (FirstTableViewCell*)[self.tableView dequeueReusableCellWithIdentifier:@"FirstTableViewCell"];
    } else  {
        cell = (FirstTableViewCell*)[self.tableView dequeueReusableCellWithIdentifier:@"FirstTableViewCell" forIndexPath:indexPath];
    }

    NSDictionary *object = [self getObjectFromIndexPath:indexPath tableView:tableView];
    if (!object) return cell;
    cell.object = celeb;
    cell.objectName.text = [object objectForKey:@"name"];

    }
    }

    return cell;
}

进行如下更改:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if ([tableView isEqual:self.searchDisplayController.searchResultsTableView]){
        if (filteredContentList.count > 0){
            return filteredContentList.count;
        }else{
            return 1; // the number of custom cells when there're no results
        }
    }else{
        return [_objects[section] count];
    }
}    

- (NSDictionary *)getObjectFromIndexPath:(NSIndexPath*)indexPath tableView:(UITableView*)tableView {
    BOOL search = [tableView isEqual:self.searchDisplayController.searchResultsTableView];
    if (search)
        return indexPath.row < filteredContentList.count? filteredContentList[indexPath.row]: theCustomCellDict; // use theCustomCellDict instead of nil
    else return indexPath.section < _objects.count?
        (indexPath.row < [_objects[indexPath.section] count]? _objects[indexPath.section][indexPath.row]: nil)
        :nil;
}    

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    BOOL isSearch = [tableView isEqual:self.searchDisplayController.searchResultsTableView];
    FirstTableViewCell *cell;
    NoResultsTableViewCell *noReultsCell;
    if ((isSearch && filteredContentList.count > 0) || !isSearch){
        cell = (FirstTableViewCell *)[self.tableView dequeueReusableCellWithIdentifier:@"FirstTableViewCell" forIndexPath:indexPath];
        NSDictionary *object = [self getObjectFromIndexPath:indexPath tableView:tableView];
        if (!object) return cell;
        cell.object = celeb;
        cell.objectName.text = [object objectForKey:@"name"];
        return cell;
    }else{
        noResultsCell = (NoResultsTableViewCell *)[self.tableView dequeueReusableCellWithIdentifier:@"NoResultsTableViewCell" forIndexPath:indexPath];
        NSDictionary *object = [self getObjectFromIndexPath:indexPath tableView:tableView];
        if (!object) return noResultsCell;
        noResultsCell.object = celeb;
        noResultsCell.objectName.text = [object objectForKey:@"name"];
        return noResultsCell
    }
}

如果您只想显示信息(例如没有找到结果'something'),您不需要使用 UITableViewCells。我会推荐你​​看看 DZNEmptyDataSet 库。

GitHub link https://github.com/dzenbot/DZNEmptyDataSet