如何对指定的数据列表使用 XLData 搜索

How to use XLData search with a specified list of data

编辑: 在反复尝试之后,我意识到使用 XLData 需要我在某个在线位置获取数据,因为搜索请求的结果来自 URL 而不是来自我的数据集。

那么,现在我的问题是,如何使用 XLData 的搜索功能来处理特定的数据列表并查询该数据集而不是在线查询某些数据?

只是事后的想法 - 输入 "a" 在应用为下面的代码时确实 return 了一些结果。那是怎么发生的呢?


我正在使用 XLData 加载要搜索和 select 来自的客户列表,但输入到搜索框中的搜索结果似乎根本不会影响搜索结果。

例如,搜索 "food"(当第一个列表项的名称中明确包含 "food" 时)不会将该客户名称过滤到搜索结果中。搜索数字 - 以数字开头的客户名称 - 没有结果。

如何抓取基于搜索词的搜索结果进行调试,看看发生了什么?

另一件事是,当加载搜索结果时,列表被截断为 9 个项目,即使有超过 1000 名客户。

这是 class 用于 select 客户(一些用户名和图像代码仍然存在于行项目中,因为我按原样使用了 XLData 的示例,但我没有显示图片):

#import "CustomersTableViewController.h"
#import "AppDelegate.h"
#import "HTTPSessionManager.h"
#import <AFNetworking/UIImageView+AFNetworking.h>

@interface UserCell : UITableViewCell

@property (nonatomic) UIImageView * userImage;
@property (nonatomic) UILabel * userName;

@end

@implementation UserCell

@synthesize userImage = _userImage;
@synthesize userName  = _userName;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code

        [self.contentView addSubview:self.userImage];
        [self.contentView addSubview:self.userName];

        [self.contentView addConstraints:[self layoutConstraints]];
    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
}

#pragma mark - Views

-(UIImageView *)userImage
{
    if (_userImage) return _userImage;
    _userImage = [UIImageView new];
    [_userImage setTranslatesAutoresizingMaskIntoConstraints:NO];
    _userImage.layer.masksToBounds = YES;
    _userImage.layer.cornerRadius = 10.0f;
    return _userImage;
}

-(UILabel *)userName
{
    if (_userName) return _userName;
    _userName = [UILabel new];
    [_userName setTranslatesAutoresizingMaskIntoConstraints:NO];
    _userName.font = [UIFont fontWithName:@"HelveticaNeue" size:15.f];

    return _userName;
}

#pragma mark - Layout Constraints

-(NSArray *)layoutConstraints{

    NSMutableArray * result = [NSMutableArray array];

    NSDictionary * views = @{ @"image": self.userImage,
                              @"name": self.userName};

    NSDictionary *metrics = @{@"imgSize":@0.0,
                              @"margin" :@10.0};

    [result addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(margin)-[image(imgSize)]-[name]"
                                                                        options:NSLayoutFormatAlignAllTop
                                                                        metrics:metrics
                                                                          views:views]];

    [result addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(margin)-[image(imgSize)]"
                                                                        options:0
                                                                        metrics:metrics
                                                                          views:views]];

    return result;
}

@end


@interface CustomersTableViewController () <UISearchControllerDelegate>

@property (nonatomic, readonly) CustomersTableViewController * searchResultController;
@property (nonatomic, readonly) UISearchController * searchController;

@end

@implementation CustomersTableViewController
@synthesize rowDescriptor = _rowDescriptor;
@synthesize popoverController = __popoverController;
@synthesize searchController = _searchController;
@synthesize searchResultController = _searchResultController;

static NSString *const kCellIdentifier = @"CellIdentifier";
NSMutableArray *customers;

- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        [self initialize];
    }
    return self;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if(self)
    {
        [self initialize];
    }

    return self;
}

- (void)initialize
{

    customers = [[NSMutableArray alloc] init];
    customers = [AppDelegate getCustomers];

    for (int i=0; i<[customers count]; i++){
        [self.dataStore addDataItem:[customers objectAtIndex:i]];
    }

    self.dataLoader =  [[XLDataLoader alloc] initWithURLString:@"/mobile/users.json" offsetParamName:@"offset" limitParamName:@"limit" searchStringParamName:@"filter"];
    self.dataLoader.delegate = self;
    self.dataLoader.storeDelegate = self;
    self.dataLoader.limit = [customers count];
    self.dataLoader.collectionKeyPath = @"";
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [self.tableView registerClass:[UserCell class] forCellReuseIdentifier:kCellIdentifier];
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];

    if (!self.isSearchResultsController){
        self.tableView.tableHeaderView = self.searchController.searchBar;
    }
    else{
        [self.tableView setContentInset:UIEdgeInsetsMake(64, 0, 0, 0)];
        [self.tableView setScrollIndicatorInsets:self.tableView.contentInset];
    }
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.searchController.searchBar sizeToFit];
}

#pragma mark - UITableViewDataSource

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UserCell *cell = (UserCell *) [tableView dequeueReusableCellWithIdentifier:kCellIdentifier forIndexPath:indexPath];;

    cell.userName.text = [customers objectAtIndex:indexPath.row];

    cell.accessoryType = [self.rowDescriptor.value isEqual:[customers objectAtIndex:indexPath.row]] ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;

    return cell;
}

#pragma mark - UITableViewDelegate

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44.0f;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    self.rowDescriptor.value = [customers objectAtIndex:indexPath.row];

    if (self.popoverController){
        [self.popoverController dismissPopoverAnimated:YES];
        [self.popoverController.delegate popoverControllerDidDismissPopover:self.popoverController];
    }
    else if ([self.parentViewController isKindOfClass:[UINavigationController class]]){
        [self.navigationController popViewControllerAnimated:YES];
    }
}

#pragma mark - XLDataLoaderDelegate

-(AFHTTPSessionManager *)sessionManagerForDataLoader:(XLDataLoader *)dataLoader
{
    return [HTTPSessionManager sharedClient];
}

#pragma mark - UISearchController

-(UISearchController *)searchController
{
    if (_searchController) return _searchController;

    self.definesPresentationContext = YES;
    _searchController = [[UISearchController alloc] initWithSearchResultsController:self.searchResultController];
    _searchController.delegate = self;
    _searchController.searchResultsUpdater = self.searchResultController;
    _searchController.searchBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    [_searchController.searchBar sizeToFit];
    return _searchController;
}


-(CustomersTableViewController *)searchResultController
{
    if (_searchResultController) return _searchResultController;
    _searchResultController = [self.storyboard instantiateViewControllerWithIdentifier:@"CustomersTableViewController"];
    _searchResultController.dataLoader.limit = 0; // no paging in search result
    _searchResultController.isSearchResultsController = YES;
    return _searchResultController;
}

@end

我最终使用了 Appcoda 描述的普通搜索栏解决方案:http://www.appcoda.com/search-bar-tutorial-ios7/

我仍然希望能够使用 XLData,因为它可以帮助处理很多其他事情,并且可以与我也在使用的 XLForms 无缝集成。

但是 Appcoda 的搜索栏实现最终足以满足我的要求。

你把这行代码搞得一团糟:

self.dataLoader =  [[XLDataLoader alloc] initWithURLString:@"/mobile/users.json" offsetParamName:@"offset" limitParamName:@"limit" searchStringParamName:@"filter"];

那个JSON文件只有9个对象!!!

此致