Tom*_*ool 3 objective-c uitableview ios parse-platform
场景=我有一个应用程序,允许用户搜索使用该服务的其他用户.在搜索页面中有一个UISearchDisplayController,当用户开始在搜索栏中输入时,tableView将以编程方式显示(就像任何其他UISearchDisplayController一样)并根据输入的内容过滤数据库中的所有用户('开头 - ").因此用户将开始键入"B ... r ...."并且用户将开始将tableView从"Brad"填充到"Brandon"等等,基于输入的文本.
问题=如何设计解析查询以实现此效果?
具体问题=
1)何时何地开始初始查询?...
PFQuery *searchQuery = [PFUser query];
[searchQuery whereKey:@"username" containsString:controller.searchBar.text];
[searchQuery orderByDescending:@"updatedAt"];
[searchQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
NSLog(@"%@", objects);
searchArray = objects;
}];
Run Code Online (Sandbox Code Playgroud)
在"searchDisplayControllerDidBeginSearch"中?
2)何时何地我将逻辑填入tableView?
PFObject *searchObject = [searchArray objectAtIndexPath:indexPath.row];
cell.nameLabel.text = searchObject[@"name"];
Run Code Online (Sandbox Code Playgroud)
在"cellForRowAtIndexPath"中?
如果有人知道这一点,可以帮助我,我很欣赏它.
这是一个简单的例子:
#import <Parse/Parse.h>
@interface MySearchController : PFQueryTableViewController
@end
Run Code Online (Sandbox Code Playgroud)
并实施
#import "MySearchController.h"
@interface MySearchController() <UISearchBarDelegate, UISearchDisplayDelegate>
@property (strong, nonatomic) IBOutlet UISearchBar *searchBar;
@property (nonatomic, strong) NSMutableArray *searchResults;
@end
@implementation MySearchController
- (id)initWithCoder:(NSCoder *)aCoder
{
self = [super initWithCoder:aCoder];
if (self) {
// get users
self.parseClassName = [PFUser parseClassName];
self.pullToRefreshEnabled = YES;
self.paginationEnabled = YES;
self.objectsPerPage = 10;
self.searchResults = [NSMutableArray new];
}
return self;
}
- (void)filterResults:(NSString *)searchTerm {
[self.searchResults removeAllObjects];
for (PFUser *user in self.objects)
{
NSString *username = user.username;
if ([[username lowercaseString] hasPrefix:[searchTerm lowercaseString]])
{
[self.searchResults addObject:user];
}
}
}
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString: (NSString *)searchString {
[self filterResults:searchString];
return YES;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return (tableView == self.tableView) ? self.objects.count : self.searchResults.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
PFUser *user = (tableView == self.tableView) ? self.objects[indexPath.row] : self.searchResults[indexPath.row];
static NSString *identifier = @"reuseIdentifier";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:identifier];
if (!cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:identifier];
}
cell.textLabel.text = user.username;
return cell;
}
@end
Run Code Online (Sandbox Code Playgroud)
关于这个让我失望的主要事情是你有两个表视图,所以你必须要小心.其中一个表视图来自原始查询,它将为您提供所有用户,即一个用户self.tableView.另一个来自搜索结果,self.searchDisplayController.searchResultsTableView.后者在搜索时是活跃的.因此,您必须为每个常规tableviewcontroller方法返回不同的值.行数是self.objects.count或self.searchResults.count.正确的用户是self.objects[indexPath.row]或self.searchResults[indexPath.row].在给定的协议方法中,很容易检查您正在处理哪个表视图,只需使用以下条件:
(tableView == self.tableView)
| 归档时间: |
|
| 查看次数: |
832 次 |
| 最近记录: |