SearchController不搜索自定义UITableViewCells

时间:2016-02-23 22:41:17

标签: ios objective-c uitableview ios9 uisearchbar

不确定标题是否有多大意义所以这里是对正在发生的事情的描述:

我有一个UITableViewController,它使用自定义UITableViewCell作为其数据。然后我手动将searchBar添加到UITableView的标题并根据本教程进行设置:http://useyourloaf.com

现在设置了searchBar,看起来它正在工作,但问题是我实际上没有得到任何结果并且表格没有正确加载(搜索结果,它加载基础数据很好)

这是我的比较代码。我知道我一定会错过一些简单的事情......

**我现在正在硬编码我的单元格的数据,一旦我能解决这个问题,这将改为核心数据模型......虽然这可能是我的问题的基础,因为我正在硬编码单元格每个IP **

@implementation CharitiesTableViewController{
NSArray *charities;
NSArray *searchResults;
}

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

    [_charityTable registerNib:[UINib nibWithNibName:@"CharityTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"charityCell"];

    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.estimatedRowHeight = 350.0;
//    self.tableView.contentInset = UIEdgeInsetsMake(-2.0f, 0.0f, 0.0f, 0.0);

    self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
    self.searchController.searchResultsUpdater = self;
    self.searchController.dimsBackgroundDuringPresentation = NO;
    self.searchController.searchBar.delegate = self;
    self.searchController.delegate = self;

    self.tableView.tableHeaderView = self.searchController.searchBar;
    self.definesPresentationContext = YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void) setColors {

}

#pragma mark - Search Controller

- (void)updateSearchResultsForSearchController:(UISearchController *)searchController {
    NSString *searchString = searchController.searchBar.text;
    [self searchForText:searchString];
    [self.tableView reloadData];
}

- (void)searchForText:(NSString *)searchText {
    NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"name contains[c] %@", searchText];
    searchResults = [charities filteredArrayUsingPredicate:resultPredicate];
}

- (void)willPresentSearchController:(UISearchController *)searchController {
    self.navigationController.navigationBar.translucent = YES;
}

-(void)willDismissSearchController:(UISearchController *)searchController {
    self.navigationController.navigationBar.translucent = NO;
}


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if (tableView == _charityTable)
    {
        return 1;
    }
    return [searchResults count];
}

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

- (CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section {
    return SectionSpacer;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return SectionSpacer;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    switch (indexPath.section) {
        case 0:{
            CharityTableViewCell *cell = (CharityTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"charityCell"];

            cell.charityImage.image = [UIImage imageNamed:@"cleanup"];
            cell.charityName.text = @"Garbage cleanup - Crowchild";
            cell.charityTagLine.text = @"City of Calgary";
            cell.charityDescriptionShort.text = @"We are rounding up anyone that wants to help clean up the grass and nearby areas close to crowchild.";

            return cell;
        }
        default:{
            UITableViewCell *cell;
            return cell;
        }
    }
}

@end

谢谢你的帮助!

1 个答案:

答案 0 :(得分:0)

我很傻....我试图这样做,而没有像指导者那样初始化数据。代码是正确的,如果有人有任何类似的问题,请确保您的数据数组实际上有数据...