【问题标题】:SearchController not searching custom UITableViewCellsSearchController 不搜索自定义 UITableViewCells
【发布时间】:2016-02-23 22:41:16
【问题描述】:

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

我有一个 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

感谢您的帮助!

【问题讨论】:

  • 您没有在任何地方初始化charities 数组或将任何数据放入其中,因此过滤它当然不会给您任何结果。即使您确实有数据,它也不会显示,因为您将硬编码数据放入表中,而不是使用 searchResults
  • 是的,我认为情况会如此,所以一旦我正确获取数据,您认为代码会起作用吗?设置是否正确?

标签: ios objective-c uitableview ios9 uisearchbar


【解决方案1】:

我很傻......我试图在没有像 com 导师所说的那样初始化数据的情况下做到这一点。代码是正确的,如果有人有任何类似的问题,请确保您的数据数组中确实有数据...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-07-17
    • 1970-01-01
    • 2020-08-04
    • 1970-01-01
    • 1970-01-01
    • 2013-04-03
    • 2010-10-04
    相关资源
    最近更新 更多