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

时间:2015-08-17 13:54:43

标签: ios objective-c uitableview uisearchdisplaycontroller

我有一个UITableView,它由一些自定义的UITableViewCells和一个连接到它的UISearchDisplayController组成。如果在搜索某些内容时没有结果,如何显示另一个自定义表视图单元格?

以下是搜索和表格视图的功能:

- (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;
}

2 个答案:

答案 0 :(得分:2)

进行如下更改:

OUTER APPLY (select *

答案 1 :(得分:1)

如果您只想显示信息(例如没有找到结果&#39;某些内容&#39; )您不需要使用UITableViewCells。我建议你看看DZNEmptyDataSet库。

GitHub链接https://github.com/dzenbot/DZNEmptyDataSet