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

Search 如何对指定的数据列表使用XLData搜索,search,xlform,Search,Xlform,编辑: 在做了更多的尝试之后,我意识到使用XLData需要我将数据放在网上的某个地方,因为搜索请求的结果来自URL,而不是我的数据集 所以,现在我的问题是,如何使用XLData的搜索功能来搜索特定的数据列表,并查询该数据集,而不是在线查询某些数据 只是事后想一想——键入a确实会返回一些结果,如下面的代码所示。那是怎么发生的呢 我使用加载要搜索和选择的客户列表,但是输入到搜索框中的搜索结果似乎根本不会影响搜索结果 例如,当第一个列表项的名称中明显包含食物时,搜索食物不会将该客户名称过滤到搜索结果中

编辑: 在做了更多的尝试之后,我意识到使用XLData需要我将数据放在网上的某个地方,因为搜索请求的结果来自URL,而不是我的数据集

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

只是事后想一想——键入a确实会返回一些结果,如下面的代码所示。那是怎么发生的呢

我使用加载要搜索和选择的客户列表,但是输入到搜索框中的搜索结果似乎根本不会影响搜索结果

例如,当第一个列表项的名称中明显包含食物时,搜索食物不会将该客户名称过滤到搜索结果中。搜索数字——以数字开头的客户名称——不会给出结果

如何捕获基于搜索词的搜索结果以进行调试并查看发生了什么

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

这是一个用于选择客户的类。一些用户名和图像代码仍然存在于行项目中,因为我使用了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描述的普通搜索栏解决方案:

我仍然希望能够使用XLData,因为它有助于处理许多其他事情,并与我正在使用的XLForms无缝集成


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

这行代码把一切都搞糟了:

self.dataLoader =  [[XLDataLoader alloc] initWithURLString:@"/mobile/users.json" offsetParamName:@"offset" limitParamName:@"limit" searchStringParamName:@"filter"];
那个JSON文件只有9个对象


致以最良好的祝愿

我认为这与self.dataStore vs self.dataLoader有关。我只是不知道怎么做。