简体   繁体   English

Parse.com:搜索TableView

[英]Parse.com: Searching a TableView

Hey together I am using Parse SDK for my App - the interface is build in Storyboard... 嘿,我在一起正在为我的App使用Parse SDK-该界面是在Storyboard中构建的...

I am having a table view which get's his data from Parse, now I want to make it searchable. 我有一个表视图,该表是从Parse获取他的数据的,现在我想使其可搜索。

I found this post and followed it but not came to a running result! 我找到了这篇文章并关注了它,但没有得到结果! :/ :/

The search results are load but I having problems to display them... 搜索结果已加载,但显示时出现问题...

I get the following errors: 我收到以下错误:

... myApp[..] *** Assertion failure in -[UISearchResultsTableView _configureCellForDisplay:forIndexPath:], /SourceCache/UIKit_Sim/UIKit-2380.17/UITableView.m:5471
... myApp[...] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'

I tried a lot around but don't come to a result, here is my code - can anybody help: 我做了很多尝试,但没有得到结果,这是我的代码-有人可以帮忙吗:

Sorry for my bad english! 对不起,我的英语不好! ;) ;)

#import "FirstLaunchViewController.h"

@interface FirstLaunchViewController ()

@end

@implementation FirstLaunchViewController

#pragma mark - UIViewController
- (void)viewDidLoad {
    [super viewDidLoad];

    [self loadObjects];

    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navbar_background.png"] forBarMetrics:UIBarMetricsDefault];

    self.searchResults = [NSMutableArray array];

}

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithClassName:@"teams"];
    self = [super initWithCoder:aDecoder];
    if (self) {
        // The className to query on
        self.parseClassName = @"teams";
        self.textKey = @"verein";

        // The key of the PFObject to display in the label of the default cell style
        // self.keyToDisplay = @"text";

        // Whether the built-in pull-to-refresh is enabled
        self.pullToRefreshEnabled = NO;

        // Whether the built-in pagination is enabled
        self.paginationEnabled = YES;

        self.loadingViewEnabled = NO;

        // The number of objects to show per page
        self.objectsPerPage = 50;
    }
    return self;
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait || UIInterfaceOrientationIsLandscape(interfaceOrientation));
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object {

    PFTableViewCell *cell = (PFTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"Cell"];

    // Configure the cell

    UIColor *color = [[UIColor alloc] initWithRed:0.0 green:0.0 blue:0.0 alpha:0.0];

    cell.detailTextLabel.backgroundColor = color;

    cell.textLabel.backgroundColor = color;

    cell.backgroundView = [ [UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"cellbackground_n.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0]];
    cell.selectedBackgroundView = [ [UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"cellbackground_down_n.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0]];
    cell.accessoryView = [[ UIImageView alloc ] initWithImage:[UIImage imageNamed:@"cellarrows.png" ]];

    if (tableView == self.tableView) {
        cell.textLabel.text = [object objectForKey:@"verein"];
    } else {

        PFUser *obj2 = [self.searchResults objectAtIndex:indexPath.row];
        PFQuery *query = [PFQuery queryWithClassName:@"teams"];
        PFObject *searchedUser = [query getObjectWithId:obj2.objectId];
        NSString *content = [searchedUser objectForKey:@"verein"];
        cell.textLabel.text = content;

        NSLog(@"Content: %@", content);

    }

    return cell;

}

-(void)filterResults:(NSString *)searchTerm {

    [self.searchResults removeAllObjects];

    PFQuery *query = [PFQuery queryWithClassName: self.parseClassName];
    [query whereKeyExists:@"verein"]; //this is based on whatever query you are trying to accomplish
    [query whereKey:@"verein" containsString:searchTerm];

    NSArray *results  = [query findObjects];

    NSLog(@"%@", results);
    NSLog(@"%u", results.count);

    [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;
    }
}


@end

The issue is that your tableView initially has no reusable cells to dequeue. 问题是您的tableView最初没有可重用的单元出队。 You need to check whether the cell returned by dequeueReusableCellWithIdentifier: is nil, and create a cell if it is. 您需要检查dequeueReusableCellWithIdentifier:返回的单元格是否为nil,如果是,则创建一个单元格。

PFTableViewCell *cell = (PFTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"Cell"];
if (!cell)
{
  cell = [[PFTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"Cell"];
}
// Configure the cell

...

This way, you are guaranteeing that cell has a value, so you can avoid returning nil from this method. 这样,您可以保证cell具有值,因此可以避免从此方法返回nil。

To handle tableView:didSelectRowAtIndexPath: correctly, you'll need to first check which tableView triggered the event, and handle it accordingly. 要正确处理tableView:didSelectRowAtIndexPath: ,您需要首先检查哪个tableView触发了该事件,并进行相应处理。 Below is an example of how this could work - you'd need to substitute your own detail view controller. 下面是一个如何工作的示例-您需要替换自己的局部视图控制器。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
  if (tableView == self.tableView) {
    [super tableView:tableView didSelectRowAtIndexPath:indexPath];
  }
  else {
    PFUser *user = PFUser *obj2 = [self.searchResults objectAtIndex:indexPath.row];
    MyDetailViewController *detailController = [[MyDetailViewController alloc] initWithObject:user];
    [self.navigationController pushViewController:datailController animated:YES];
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM