0

EDIT: さらにいじった後、XLDataを使用するにはオンラインのどこかにデータが必要であることに気付きました。これは、検索がデータセットではなくURLからの結果を要求したためです。

それで、私の質問は、特定のデータのリストで XLData の検索機能を使用し、オンラインのデータではなくそのデータ セットを照会するにはどうすればよいかということです。

後付け - 以下のコードとして適用すると、「a」と入力するといくつかの結果が返されました。それで、それはどのように起こったのですか?


XLDataを使用して、検索して選択する顧客のリストを読み込んでいますが、検索ボックスに入力された検索結果は検索結果にまったく影響を与えないようです。

たとえば、「食品」を検索すると (最初のリスト項目の名前に明らかに「食品」が含まれている場合)、その顧客名は検索結果にフィルターされません。また、数字 (数字で始まる顧客名) を検索しても結果が得られません。

検索語に基づいて検索結果をキャッチしてデバッグし、何が起こっているかを確認するにはどうすればよいですか?

もう 1 つの問題は、検索結果が読み込まれると、1000 人以上の顧客がいるにもかかわらず、リストが 9 項目に切り捨てられることです。

ここに画像の説明を入力

これは、顧客を選択するためのクラスです (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
4

2 に答える 2

0

Appcoda で説明されているように、通常の検索バー ソリューションを使用することになりました: http://www.appcoda.com/search-bar-tutorial-ios7/

XLData は他の多くのことに役立ち、私も使用している XLForms とシームレスに統合されるため、引き続き XLData を使用できるようにしたいと考えています。

しかし、Appcoda の検索バーの実装は、私の要件には十分でした。

于 2015-10-21T17:52:35.297 に答える