数据不会显示在搜索显示控制器中

时间:2015-08-15 06:18:15

标签: ios uitableview uisearchdisplaycontroller tableviewcell

我在我的应用程序中使用搜索栏和搜索显示控制器以及tableview,我使用自定义单元格查看我的tableview。我使用restful web服务加载数据,并有两个不同的json提要。 (一个用于普通的tableview,一个用于搜索结果)。我附上了我的代码。希望你的帮助。

这是.h文件

enter image description here

这是.m文件

 #import "MainViewController.h"

@interface MainViewController ()

@end

@implementation MainViewController
@synthesize homepost, homeposts, searchpost, searchposts, isFiltered, searchresults,searchtext,searchDisplayController;

- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadhomeviedata];

//    [[self maintableview] registerClass:[MainTableViewCell class] forCellReuseIdentifier:@"cellidentifier"];


    // Do any additional setup after loading the view, typically from a nib.
}


- (void)loadhomeviedata
{
    homepost = nil;
    NSString *mainurl = [NSString stringWithFormat:@"http://www.parhlo.com/api/get_posts/?count=30"];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:mainurl parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {

        homeposts = (NSDictionary *)responseObject;
        homepost = [NSMutableArray array];
        NSArray *result = [homeposts objectForKey:@"posts"];
        for (NSDictionary *all in result)
        {
            Home *sweethome = [Home new];
            sweethome.title = [all objectForKey:@"title"];


            [homepost addObject:sweethome];
            [self.maintableview reloadData];

        }


    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

    }];


}



- (void)filterContentForSearchText:(NSString *)searchText scope:(NSString *)scope
{
    searchpost = nil;
    NSString *searchurl = [NSString stringWithFormat:@"http://www.parhlo.com/api/get_search_results/?s=%@&count=30", searchText];
    AFHTTPRequestOperationManager *searchmanager = [AFHTTPRequestOperationManager manager];
    [searchmanager GET:searchurl parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {

        searchposts = (NSDictionary *)responseObject;
        searchpost = [NSMutableArray array];
        NSArray *searchresult = [searchposts objectForKey:@"posts"];
        for(NSDictionary *allsearch in searchresult)
        {
            Home *sweetsearch = [Home new];
            sweetsearch.title = [allsearch objectForKey:@"title"];


            [searchpost addObject:sweetsearch];

            NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"SELF.title LIKE[cd] %@", searchText];
            searchresults = [searchpost filteredArrayUsingPredicate:resultPredicate];

            NSLog(@"%@", searchresults);



            [self.maintableview reloadData];





//            searchresults = [searchpost filteredArrayUsingPredicate:resultPredicate];
//            NSLog(@"%@",searchresults);

        }


    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

    }];




}

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

    return YES;
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == self.searchDisplayController.searchResultsTableView) {
        NSLog(@"%i", searchresults.count);
        return [searchresults count];
    }
    else{

        return [homepost count];

    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MainTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellidentifier"];


    Home *newhome = nil;



    if (tableView == self.searchDisplayController.searchResultsTableView) {
        newhome = [searchresults objectAtIndex:indexPath.row];

    }
    else{
        newhome = [homepost objectAtIndex:indexPath.row];
//        NSLog(@"%@", homepost);
    }

    cell.titlelabel.text = newhome.title;

    return cell;

}
@end

1 个答案:

答案 0 :(得分:0)

我不知道你在哪里声明控制器的代表,你应该做这样的事情:

 searchResultsController.tableView.dataSource = self;

 searchResultsController.tableView.delegate = self;

和此:

@interface MainViewController () <UISearchResultsUpdating, UISearchBarDelegate>

您可能需要在viewDidLoad中注册自定义UITableViewCell类,如下所示:

[[self tableView] registerClass:[CustomTableViewCell class] forCellReuseIdentifier:@"come.somespecial.name"];