编辑: 在摆弄了更多之后,我意识到使用 XLData 需要我将数据放在在线的某个地方,因为搜索从 URL 而不是从我的数据集请求结果。
所以,现在我的问题是,如何将 XLData 的搜索功能与特定的数据列表一起使用并查询该数据集而不是在线查询某些数据?
只是事后的想法 - 键入“a”确实会在应用如下代码时返回一些结果。那是怎么发生的呢?
我正在使用XLData加载要搜索和选择的客户列表,但在搜索框中输入的搜索结果似乎根本不会影响搜索结果。
例如,搜索“food”(当第一个列表项的名称中明确包含“food”时)不会将该客户名称过滤到搜索结果中。搜索数字 - 以数字开头的客户名称 - 没有结果。
如何根据搜索词捕获搜索结果以对其进行调试并查看发生了什么?
另一件事是,当搜索结果加载时,列表被截断为 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