0

我创建了一个从 .xml 文件解析的 RSS 阅读器。我正在尝试创建搜索栏和搜索显示控制器,但不确定如何在 UITableView 中搜索 objectForKey“标题”或 objectForKey“摘要”。

任何帮助将不胜感激。

numberOfRowsInSection 和 cellForRowAtIndexPath 看起来像这样:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return self.parseResults.count;
}

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

    //Check if cell is nil. If it is create a new instance of it
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }
    // Configure titleLabel
    cell.textLabel.text = [[self.parseResults objectAtIndex:indexPath.row] objectForKey:@"title"];
    cell.textLabel.numberOfLines = 2;
    //Configure detailTitleLabel
     cell.detailTextLabel.text = [[self.parseResults objectAtIndex:indexPath.row] objectForKey:@"summary"];

    cell.detailTextLabel.numberOfLines = 2;

    //Set accessoryType
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    //Set font and style
     cell.selectionStyle = UITableViewCellSelectionStyleGray;
     cell.textLabel.font = [UIFont boldSystemFontOfSize:14];

    return cell;
}

我最近根据一个建议尝试关注这个示例项目 - https://github.com/deepthit/TableViewSearch.git 。
然后我的代码如下所示:

@interface QldRecentJudgmentsViewController () {

    __strong NSArray *mFilteredArray_;
    __strong UISearchBar *mSearchBar_;
    __strong UISearchDisplayController *mSearchDisplayController_;
}

@end

 @implementation ViewController
 @synthesize parseResults = _parseResults, HUD;


- (void)viewDidLoad {
    [super viewDidLoad];


    mSearchBar_ = [[UISearchBar alloc] initWithFrame:CGRectMake(0,
                                                            0,
                                                            self.view.bounds.size.width,
                                                            44)];

     mSearchBar_.delegate = self;
     mSearchBar_.placeholder = @"search";
     self.tableView.tableHeaderView = mSearchBar_;

     mSearchDisplayController_ = [[UISearchDisplayController alloc] initWithSearchBar:mSearchBar_
                                                              contentsController:self];
     mSearchDisplayController_.searchResultsDelegate = self;
     mSearchDisplayController_.searchResultsDataSource = self;
     mSearchDisplayController_.delegate = self;

 }



#pragma mark - Table view data source

 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
 }

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    //return self.parseResults.count;

    if (tableView == self.searchDisplayController.searchResultsTableView ||
    [mFilteredArray_ count] > 0)
    {
        return [mFilteredArray_ count];
    }
    return parseResults.count;

}


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

    id result;
    if (tableView == self.searchDisplayController.searchResultsTableView ||
    [mFilteredArray_ count] > 0)
    {
        result = [mFilteredArray_ objectAtIndex:indexPath.row];
    }
    else
    {
        result = [parseResults objectAtIndex:indexPath.row];
    }


    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    //Check if cell is nil. If it is create a new instance of it
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];

        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    }
    // Configure titleLabel
    cell.textLabel.text = [[self.parseResults objectAtIndex:indexPath.row] objectForKey:@"title"];
    cell.textLabel.numberOfLines = 2;
    //Configure detailTitleLabel
    cell.detailTextLabel.text = [[self.parseResults objectAtIndex:indexPath.row] objectForKey:@"summary"];

    cell.detailTextLabel.numberOfLines = 2;

    //Set accessoryType
    //cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    //Set font and style
    cell.selectionStyle = UITableViewCellSelectionStyleGray;
    cell.textLabel.font = [UIFont boldSystemFontOfSize:14];

    return cell;
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *url = [[self.parseResults objectAtIndex:indexPath.row] objectForKey:@"link"];
    NSString *title = [[self.parseResults objectAtIndex:indexPath.row] objectForKey:@"title"];
    WebViewController *viewController = [[WebViewController alloc] initWithURL:url title:title];
    [self.navigationController pushViewController:viewController animated:YES];
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

}

#pragma mark - UISearchBarDelegate

- (void)searchBar:(UISearchBar *)searchBar
textDidChange:(NSString *)searchText {
    if ([searchText length] == 0)
    {
        [self.tableView reloadData];
        return;
    }

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.title contains[cd] %@ OR SELF.summary contains[cd] %@", searchText, searchText];
    mFilteredArray_ = [self.parseResults filteredArrayUsingPredicate:predicate];

    [self.tableView reloadData];
}

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
    mFilteredArray_ = nil;
    [self.tableView reloadData];
}

但是,当我遵循此操作时,RSS 提要不再加载到表格视图中,因此没有结果。然而,当我尝试搜索时,它没有正确搜索“标题”或“摘要”,并且搜索结果没有正确显示 - 在搜索某些内容并获得结果后,单元格没有整齐地对齐。此外,在 tableview 中查看 RSS 的唯一方法是搜索任何通用字符串,但是一旦您在搜索栏中按取消,RSS 提要就会消失并显示一个空的 tableview。

感谢您提前提供任何帮助。

4

0 回答 0