0

问题部分解决,见帖子结尾:

UITableView在不工作时显示搜索结果。table保持空白,不显示任何内容results,但searching必须工作,因为当我search处理不在其中的内容时,table我会立即"No Results"table.

注意:在我IB用我connectdelegate.UISearchBarConnection InspectorController

希望你能帮忙。

米:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return (tableView == self.searchDisplayController.searchResultsTableView) ? [self.seriesArrayFiltered count] : [self.seriesArray count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"SeriesCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    Serie *aSerie = nil;

    if (tableView == self.searchDisplayController.searchResultsTableView) {
        NSLog(@"search is displaying");
        aSerie = self.seriesArrayFiltered[[indexPath row]];
    } else {
        NSLog(@"search is NOT displaying");
        aSerie = self.seriesArray[[indexPath row]];
    }

    UILabel *valueLbl = (UILabel *)[cell viewWithTag:101];
    valueLbl.text = aSerie.name;

    return cell;
}


-(void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope {

    [self.seriesArrayFiltered removeAllObjects];

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.name contains[c] %@",searchText];
    self.seriesArrayFiltered = [NSMutableArray arrayWithArray:[self.seriesArray filteredArrayUsingPredicate:predicate]];
}


-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
    [self filterContentForSearchText:searchString scope:
     [[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];

    return YES;
}


-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption
{
    [self filterContentForSearchText:self.searchDisplayController.searchBar.text scope:
     [[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:searchOption]];

    return YES;
}

H:

#import <UIKit/UIKit.h>

@interface SeriesTableVC : UITableViewController <UISearchDisplayDelegate, UISearchBarDelegate>

@property (nonatomic, strong) NSArray *seriesArray;
@property (nonatomic, strong) NSMutableArray *seriesArrayFiltered;

@property (weak, nonatomic) IBOutlet UISearchBar *seriesSearchBar;

@end

当我更改以下代码时,我现在可以看到搜索结果,但我想知道为什么无法识别标签。

在:cellForRowAtIndexPath

//UILabel *valueLbl = (UILabel *)[cell viewWithTag:101];
//valueLbl.text = aSerie.name;

cell.textLabel.text = aSerie.name;
4

1 回答 1

0

您创建的单元格不包含标签,因此

UILabel *valueLbl = (UILabel *)[cell viewWithTag:101];

返回nil,设置valueLbl.text = aSerie.name;无效。

创建单元格时,您必须创建标签并将其作为子视图添加到单元格中:

if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    UILabel *valueLbl = [[UILabel alloc] initWithFrame:...];
    [cell.contentView addSubview:valueLbl];
}
于 2013-03-01T19:23:21.140 回答