使用Parse iOS搜索数组

时间:2014-07-08 22:13:59

标签: ios objective-c parse-platform

我正在尝试搜索使用Parse与数据库通信的tableview。无论如何,我认为我的过滤功能有问题。我添加了一些NSLog,但是当我确实应该有一些值时它们打印0。我看了许多不同的教程/论坛,但由于某种原因没有任何工作。我总是得不到任何结果。我相信问题出在filterResults方法中。我得到的输出在下面。

2014-07-08 14:11:15.488 Truthly[3674:184774] (
)
2014-07-08 14:11:15.488 Truthly[3674:184774] 0
2014-07-08 14:11:15.488 Truthly[3674:184774] results^
2014-07-08 14:11:15.489 Truthly[3674:184774] (
)
2014-07-08 14:11:15.490 Truthly[3674:184774] 0
2014-07-08 14:11:15.490 Truthly[3674:184774] search results^
2014-07-08 14:11:16.554 Truthly[3674:184774] Warning: A long-running Parse operation is being executed on the main thread. 
 Break on warnParseOperationOnMainThread() to debug.
2014-07-08 14:11:16.709 Truthly[3674:184774] (
)
2014-07-08 14:11:16.709 Truthly[3674:184774] 0
2014-07-08 14:11:16.710 Truthly[3674:184774] results^
2014-07-08 14:11:16.710 Truthly[3674:184774] (
)
2014-07-08 14:11:16.710 Truthly[3674:184774] 0
2014-07-08 14:11:16.711 Truthly[3674:184774] search results^
2014-07-08 14:11:17.798 Truthly[3674:184774] Warning: A long-running Parse operation is being executed on the main thread. 
 Break on warnParseOperationOnMainThread() to debug.
2014-07-08 14:11:17.964 Truthly[3674:184774] (
)
2014-07-08 14:11:17.965 Truthly[3674:184774] 0
2014-07-08 14:11:17.965 Truthly[3674:184774] results^
2014-07-08 14:11:17.965 Truthly[3674:184774] (
)
2014-07-08 14:11:17.966 Truthly[3674:184774] 0
2014-07-08 14:11:17.966 Truthly[3674:184774] search results^

这是我的m档案。

#import "AllDataTableViewController.h"
#import <Parse/Parse.h>

@interface AllDataTableViewController ()  <UISearchDisplayDelegate, UISearchBarDelegate> {

}
@end

@implementation AllDataTableViewController : PFQueryTableViewController


@synthesize searchBar;
@synthesize searchController;
@synthesize searchResults;

-(void)viewDidAppear:(BOOL)animated{

    if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"popAgain"] isEqualToString:@"true"])
    {

        [self.navigationController popViewControllerAnimated:YES];
        NSString *popAgain = @"false";
        [[NSUserDefaults standardUserDefaults] setObject:popAgain forKey:@"popAgain"];

    }
}




- (id)initWithCoder:(NSCoder *)aCoder
{
    self = [super initWithCoder:aCoder];
    if (self) {
        // The className to query on
        self.className = @"TruthIsData";

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

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

        // Whether the built-in pagination is enabled
        self.paginationEnabled = NO;
    }
    return self;

}



-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    if (tableView == self.tableView) {

        return self.objects.count;

    } else {
        //NSLog(@"how many in search results");
        //NSLog(@"%@", self.searchResults.count);
        return self.searchResults.count;

    }

}




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

    [self.searchResults removeAllObjects];

    PFQuery *query = [PFQuery queryWithClassName:@"truthIsData"];
    //[query whereKey:@"truthIsName" containsString:searchTerm];

    NSArray *results  = [query findObjects];

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

   [self.searchResults addObjectsFromArray:results];

    NSPredicate *searchPredicate =
    [NSPredicate predicateWithFormat:@"ANY SELF == %@", searchTerm];
    searchResults = [NSMutableArray arrayWithArray:[results filteredArrayUsingPredicate:searchPredicate]];

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





}




- (PFQuery *)queryForTable
{
    PFQuery *query = [PFQuery queryWithClassName:self.className];
    if ([self.objects count] == 0) {
        query.cachePolicy = kPFCachePolicyCacheThenNetwork;
    }

    return query;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object
{

    static NSString *simpleTableIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];



    // Configure the cell
    if (tableView == self.tableView) {
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:simpleTableIdentifier];
        }

        cell.textLabel.text = [object objectForKey:@"truthIsName"];
        cell.detailTextLabel.text = [NSString stringWithFormat:@"User: %@", [object objectForKey:@"username"]];

    }
    if (tableView == self.searchDisplayController.searchResultsTableView) {

        PFObject* object = self.searchResults[indexPath.row];

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:simpleTableIdentifier];
        }

        cell.textLabel.text = [object objectForKey:@"truthIsName"];
        cell.detailTextLabel.text = [NSString stringWithFormat:@"User: %@", [object objectForKey:@"username"]];
    }



    return cell;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {


    PFObject *obj = [self.objects objectAtIndex:indexPath.row];

    NSString *quickId = obj.objectId;

    [[NSUserDefaults standardUserDefaults] setObject:quickId forKey:@"selectedTI"];
    //NSLog(quickId);


   [self performSegueWithIdentifier:@"selectedRequest" sender:self];


}

-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
    [self filterResults:searchString];
    return YES;
}



@end

我觉得我已经尝试了所有东西,但我无法让它发挥作用。任何建议都很棒,谢谢!

1 个答案:

答案 0 :(得分:0)

设置表'self.parse.class时,您的Parse类(表)设置为“TruthIsData” 但在你的过滤方法中,它是“truthIsData”。

查询Parse类区分大小写,因此无论哪个名称正确,您都必须更改错误的名称,并且您的对象将会出现。