我试图让搜索过滤器适用于我的tableview,当我点击搜索栏并且返回此错误时,它会崩溃应用程序
2014-03-30 14:44:08.676 BookStore [16156:90b] *由于未捕获的异常'NSInvalidArgumentException'终止应用程序,原因:' - [PFObject rangeOfString:options:]:发送到实例的无法识别的选择器0x9da1a40' * 首先抛出调用堆栈:
这是代码:
//
// PDFTableViewController.m
// BookStore
//
// Created by Danijel Kujundzic on 3/23/14.
// Copyright (c) 2014 Danijel Kujundzic. All rights reserved.
//
#import "PDFTableViewController.h"
#import "PDFDetailViewController.h"
@interface PDFTableViewController ()<UISearchBarDelegate , UISearchDisplayDelegate>
{
NSMutableArray * filteredStrings;
BOOL isFiltered;
}
@end
@implementation PDFTableViewController
@synthesize PDFtableView;
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self performSelector:@selector(RetrievePDFParseData)];
self.SearchBar.delegate =self;
self.PDFtableView.delegate=self;
self.PDFtableView.dataSource =self;
filteredStrings = [[NSMutableArray alloc] initWithArray:PDFArray];
}
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
if (searchText.length ==0) {
isFiltered= NO;
[filteredStrings removeAllObjects];
[self.tableView reloadData];
[searchBar resignFirstResponder];
}
else
{
isFiltered = YES;
if([PDFArray count]!=0)
{
NSPredicate *p=[NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) {
NSString *s=evaluatedObject;
return ([s rangeOfString:searchBar.text options:NSCaseInsensitiveSearch].location !=NSNotFound);
}];
filteredStrings= [NSMutableArray arrayWithArray:[PDFArray filteredArrayUsingPredicate:p]];
//table reload
[self.tableView reloadData];
}
}
}
-(void) RetrievePDFParseData {
PFQuery * getPDF = [PFQuery queryWithClassName:@"PDFTableView"];
[getPDF findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if(!error) {
PDFArray =[[NSArray alloc] initWithArray: objects];
}
[PDFtableView reloadData];
}];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (isFiltered) {
return [filteredStrings count];
}
return [PDFArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString * CellIdentifier = @"Cell";
UITableViewCell * cell = [PDFtableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell ==nil) {
cell = [[ UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"Cell"];
}
if (!isFiltered) {
PFObject * tempObject = [PDFArray objectAtIndex:indexPath.row];
cell.textLabel.text = [tempObject objectForKey:@"PDFName"];
cell.detailTextLabel.text= [tempObject objectForKey:@"Author"];
}
if (isFiltered)
{
PFObject *filteredObject= [[filteredStrings objectAtIndex:indexPath.row]initWithArray:PDFArray];
cell.textLabel.text =[filteredObject objectForKey:@"PDFName"];
cell.detailTextLabel.text= [filteredObject objectForKey:@"Author"];
NSLog(@"%@", filteredObject);
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[self performSegueWithIdentifier:@"detailSegue" sender:self];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([segue.identifier isEqualToString:@"detailSegue"]){
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
PDFDetailViewController *detailVC = (PDFDetailViewController *)segue.destinationViewController;
NSLog(@"Bookarray=%@", PDFArray);
NSLog(@"BookIndex=%@", [PDFArray objectAtIndex:indexPath.row]);
detailVC.PDFna=[[PDFArray objectAtIndex:indexPath.row]objectForKey:@"PDFName"];
detailVC.PDFdes= [[PDFArray objectAtIndex:indexPath.row]objectForKey:@"About"];
detailVC.downloadfile=[[PDFArray objectAtIndex:indexPath.row]objectForKey:@"PDFFile"];
}
}
@end
答案 0 :(得分:1)
您认为是NSStrings的对象数组实际上是PFObjects。 PDFArray包含一个PFObject数组。您可能想要在UISearchBarDelegate方法中创建的谓词中抓取某个属性,
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
像NSString *s = evaluatedObject[@"PDFName"];
这样的东西,或其他类似的东西会使你实际上过滤PDF文件的名称。
答案 1 :(得分:1)
错误在searchBar:textDidChange:
方法中。在这种情况下,谓词返回PFObject
,而不是NSString
。因此,您必须在evaluatedObject
对象中搜索您需要的NSString
对象。
这些是有问题的行:
NSString *s=evaluatedObject;
return ([s rangeOfString:searchBar.text options:NSCaseInsensitiveSearch].location !=NSNotFound);
rangeOfString
引发了异常,因为PFObject
没有回复rangeOfString
。
在其他语言中,编译器有时会警告您这种情况。在Objective-C中,在处理任何类型的对象时必须小心:id
。如果特定对象在编译时响应消息,则编译器不会进行检查。但是当Objective-C运行时在运行时执行此操作时,会出现崩溃。
在Objective-C中阅读有关对象和消息的更多信息: https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/WorkingwithObjects/WorkingwithObjects.html