Objective c Parse.com-如何使用UISearchDisplayController获取PFQueryTableViewController、分页?
我正在尝试将搜索添加到应用程序中。我有以下代码到目前为止与搜索工作。下面代码的问题是分页不再工作。有一个简单的问题是,当返回“section+1中的行数”时,应用程序会崩溃,并出现[\uu NSArrayM objectAtIndex:]错误。如何使分页工作 //MyTableController.hObjective c Parse.com-如何使用UISearchDisplayController获取PFQueryTableViewController、分页?,objective-c,parse-platform,uisearchdisplaycontroller,Objective C,Parse Platform,Uisearchdisplaycontroller,我正在尝试将搜索添加到应用程序中。我有以下代码到目前为止与搜索工作。下面代码的问题是分页不再工作。有一个简单的问题是,当返回“section+1中的行数”时,应用程序会崩溃,并出现[\uu NSArrayM objectAtIndex:]错误。如何使分页工作 //MyTableController.h #import <Parse/Parse.h> @interface MyTableController : PFQueryTableViewController @end #导
#import <Parse/Parse.h>
@interface MyTableController : PFQueryTableViewController
@end
#导入
@接口MyTableController:PFQueryTableViewController
@结束
//MyTableController.m
#import "MyTableController.h"
@interface MyTableController() <UISearchDisplayDelegate> {
}
@property (nonatomic, strong) UISearchBar *searchBar;
@property (nonatomic, strong) UISearchDisplayController *searchController;
@property (nonatomic, strong) NSMutableArray *searchResults;
@end
@implementation MyTableController
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
self.className = @"Todo";
self.keyToDisplay = @"text";
self.pullToRefreshEnabled = YES;
self.paginationEnabled = YES;
self.objectsPerPage = 5;
}
return self;
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
self.tableView.tableHeaderView = self.searchBar;
self.searchController = [[UISearchDisplayController alloc] initWithSearchBar:self.searchBar
contentsController:self];
self.searchController.searchResultsDataSource = self;
self.searchController.searchResultsDelegate = self;
self.searchController.delegate = self;
CGPoint offset = CGPointMake(0, self.searchBar.frame.size.height);
self.tableView.contentOffset = offset;
self.searchResults = [NSMutableArray array];
}
- (void)viewDidUnload
{
[super viewDidUnload];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (void)filterResults:(NSString *)searchTerm {
[self.searchResults removeAllObjects];
PFQuery *query = [PFQuery queryWithClassName: self.className];
[query whereKey:@"text" containsString:searchTerm];
NSArray *results = [query findObjects];
[self.searchResults addObjectsFromArray:results];
}
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
[self filterResults:searchString];
return YES;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (tableView == self.tableView) {
return self.objects.count;
} else {
return self.searchResults.count ;
}
}
#pragma mark - Parse
- (void)objectsDidLoad:(NSError *)error {
[super objectsDidLoad:error];
}
- (void)objectsWillLoad {
[super objectsWillLoad];
}
- (PFQuery *)queryForTable {
PFQuery *query = [PFQuery queryWithClassName:self.className];
if ([self.objects count] == 0) {
query.cachePolicy = kPFCachePolicyCacheThenNetwork;
}
[query orderByAscending:@"priority"];
return query;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [object objectForKey:@"text"];
cell.detailTextLabel.text = [NSString stringWithFormat:@"Priority: %@", [object objectForKey:@"priority"]];
return cell;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[super tableView:tableView didSelectRowAtIndexPath:indexPath];
}
@end
#导入“MyTableController.h”
@接口MyTableController(){
}
@属性(非原子,强)UISearchBar*搜索栏;
@属性(非原子,强)UISearchDisplayController*searchController;
@属性(非原子,强)NSMutableArray*搜索结果;
@结束
@MyTableController的实现
-(id)initWithStyle:(UITableViewStyle)样式
{
self=[super initWithStyle:style];
如果(自我){
self.className=@“Todo”;
self.keyToDisplay=@“text”;
self.pullToRefreshanbled=是;
self.paginationEnabled=是;
self.objectsPerPage=5;
}
回归自我;
}
#pragma标记-视图生命周期
-(无效)viewDidLoad
{
[超级视图下载];
self.searchBar=[[UISearchBar alloc]initWithFrame:CGRectMake(0,0,self.view.frame.size.width,44)];
self.tableView.tableHeaderView=self.searchBar;
self.searchController=[[UISearchDisplayController alloc]initWithSearchBar:self.searchBar
内容控制器:self];
self.searchController.searchResultsDataSource=self;
self.searchController.searchResultsDelegate=self;
self.searchController.delegate=self;
CGPoint offset=CGPointMake(0,self.searchBar.frame.size.height);
self.tableView.contentOffset=偏移量;
self.searchResults=[NSMutableArray];
}
-(无效)视图卸载
{
[超级视频下载];
}
-(布尔)应自动旋转指针面定向:(UIInterfaceOrientation)interfaceOrientation
{
返回(interfaceOrientation==UIInterfaceOrientationGraphic);
}
-(无效)未收到记忆警告
{
[超级记忆警告];
}
-(void)filterResults:(NSString*)searchTerm{
[self.searchResults removeAllObjects];
PFQuery*query=[pfqueryquerywithclassname:self.className];
[查询whereKey:@“text”包含字符串:searchTerm];
NSArray*results=[query findObjects];
[self.searchResults addObjectsFromArray:results];
}
-(BOOL)searchDisplayController:(UISearchDisplayController*)控制器应重新加载搜索字符串的表:(NSString*)搜索字符串{
[自我筛选结果:搜索字符串];
返回YES;
}
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节{
if(tableView==self.tableView){
返回self.objects.count;
}否则{
返回self.searchResults.count;
}
}
#pragma标记-解析
-(void)objectsDidLoad:(NSError*)错误{
[超级对象sdidload:错误];
}
-(void)objectsWillLoad{
[超级对象swillload];
}
-(PFQuery*)queryForTable{
PFQuery*query=[pfqueryquerywithclassname:self.className];
如果([self.objects count]==0){
query.cachePolicy=kPFCachePolicyCacheThenNetwork;
}
[查询顺序升序:@“优先级”];
返回查询;
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath对象:(PFObject*)对象{
静态NSString*CellIdentifier=@“Cell”;
UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
如果(单元格==nil){
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle重用标识符:CellIdentifier];
}
cell.textlab.text=[object objectForKey:@“text”];
cell.detailTextLabel.text=[NSString stringWithFormat:@“Priority:%@,[object objectForKey:@“Priority”];
返回单元;
}
#pragma标记-表视图委托
-(void)tableView:(UITableView*)tableView未选择RowatineXpath:(NSIndexPath*)indexPath
{
[super tableView:tableView didSelectRowAtIndexPath:indexath];
}
@结束
在您自己实现的numberofrowsinssection
中,必须返回numberOfRows+1。但是,您必须编写代码,因为您的单元格比查询返回的单元格多。查看您的heightforrowatinexpath
和其他方法:可以调用[self.objects objectAtIndex:self.objects.count]。我的意思是,如果indexPath.row==self.objects.count
,它就是一个“加载更多”单元格。
如果重写将选择RowatineXpath
或DidSelectRowatineXpath
,则必须在方法的顶部执行此操作
[super tableView:tableView didSelectRowAtIndexPath:indexPath];
或添加此项(首选前一种情况)
自定义放置在-(PFTableViewCell*)tableView:(UITableView*)tableView cellForNextPageAtIndexPath:(NSIndexPath*)indexPath中的加载更多单元格
我正在使用
-(PFTableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath
而不是
-(UITableView单元格*)tableView:(UITableView*)tableView单元格for行索引路径:
if (indexPath.row == self.objects.count)
{
[self loadNextPage];
}