如何在UItableview中将搜索栏和搜索显示添加到rss源

时间:2013-11-21 21:14:11

标签: ios uitableview rss uisearchbar searchdisplaycontroller

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

非常感谢任何帮助。

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中,因此没有结果。然而,当我尝试搜索时,它没有正确搜索“标题”或“摘要”,并且搜索结果显示不正确 - 搜索某些内容并获得结果后,单元格并未整齐排列。此外,在tableview中查看RSS的唯一方法是搜索任何通用字符串,但是一旦在搜索栏中按下取消,RSS源就会消失并显示一个空的tableview。

提前感谢您的帮助。

0 个答案:

没有答案