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

时间:2015-10-21 07:59:36

标签: search xlform

修改 在摆弄了一些之后,我意识到使用XLData要求我将数据放在网上某处,因为搜索从URL而不是从我的数据集请求结果。

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

只是一个事后的想法 - 打字" a"当作为下面的代码应用时,确实返回了一些结果。那怎么发生呢?

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

例如,搜索"食物" (当第一个列表项清楚地显示" food"在其名称中)时,不会将该客户名称过滤到搜索结果中。搜索数字 - 以数字开头的客户名称 - 没有结果。

如何根据搜索字词捕获搜索结果以进行调试并查看正在进行的操作?

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

enter image description here

这是用于选择客户的类(某些用户名和图像代码仍然存在于订单项中,因为我原样使用了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

2 个答案:

答案 0 :(得分:0)

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

我仍然希望能够使用XLData,因为它可以帮助解决许多其他问题,并与我正在使用的XLForms无缝集成。

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

答案 1 :(得分:0)

你正在弄乱这一行代码:

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

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

祝你好运