TableView中的搜索栏:当键入搜索文本字符串的最后一个字符时,应用程序崩溃

时间:2014-09-24 20:42:01

标签: uitableview ios7 xcode5 uisearchbar

当输入搜索文本字符串的最后一个字符且错误

时,在tableview应用程序顶部的故事板中添加了搜索栏崩溃

由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:' - [PFUser copyWithZone:]:发送到实例的无法识别的选择器

- (void)viewDidLoad
{
[super viewDidLoad];

PFQuery *query = [PFUser query];
[query orderByAscending:@"username"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
    if (error){
        NSLog(@"Error:%@ %@", error, [error userInfo]);
    }

    else {
        self.allUsers = objects;
        self.searchResults = [[NSArray alloc] init];
                    [self.tableView reloadData];        }
}];

 - (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope {

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"username like %@", searchText];
self.searchResults = [self.allUsers filteredArrayUsingPredicate:predicate];
}

 -(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
[self filterContentForSearchText:searchString scope:[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];

return YES;
}

 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

return 1;
 }

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


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

    return [self.searchResults count];
} else {

    //[self filterData];
    return [self.allUsers count];
  }

  }


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

static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

if (tableView == self.searchDisplayController.searchResultsTableView) {
    cell.textLabel.text = [self.searchResults objectAtIndex:indexPath.row];

} else {

    PFUser *user = [self.allUsers objectAtIndex:indexPath.row];
    cell.textLabel.text = user.username;
        }


return cell;

 }

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

  {

if (tableView == self.tableView){

    self.title = [self.allUsers objectAtIndex:indexPath.row];
} else {

    self.title = [self.searchResults objectAtIndex:indexPath.row];
}

  }

编辑:

当我在lldb提示符下键入bt时,这就是我得到的

 * thread #1: tid = 0x86e99, 0x029b68b9 libobjc.A.dylib`objc_exception_throw, queue = 'com.apple.main-thread', stop reason = breakpoint 1.1
frame #0: 0x029b68b9 libobjc.A.dylib`objc_exception_throw
frame #1: 0x02cd4243 CoreFoundation`-[NSObject(NSObject) doesNotRecognizeSelector:] + 275
frame #2: 0x02c2750b CoreFoundation`___forwarding___ + 1019
frame #3: 0x02c270ee CoreFoundation`__forwarding_prep_0___ + 14
frame #4: 0x029c8bcd libobjc.A.dylib`-[NSObject copy] + 41
frame #5: 0x018317b6 UIKit`-[UILabel _setText:] + 129
frame #6: 0x0183194d UIKit`-[UILabel setText:] + 40
* frame #7: 0x000138d8 -[EditFriendsViewController tableView:cellForRowAtIndexPath:](self=0x0aca3e50, _cmd=0x01e3b31f, tableView=0x0baed600, indexPath=0x0ac92b70) + 456 at EditFriendsViewController.m:204
frame #8: 0x0176f11f UIKit`-[UITableView _createPreparedCellForGlobalRow:withIndexPath:] + 412
frame #9: 0x0176f1f3 UIKit`-[UITableView _createPreparedCellForGlobalRow:] + 69
frame #10: 0x01750ece UIKit`-[UITableView _updateVisibleCellsNow:] + 2428
frame #11: 0x017656a5 UIKit`-[UITableView layoutSubviews] + 213
frame #12: 0x016e5964 UIKit`-[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 355
frame #13: 0x029c882b libobjc.A.dylib`-[NSObject performSelector:withObject:] + 70
frame #14: 0x00aa545a QuartzCore`-[CALayer layoutSublayers] + 148
frame #15: 0x00a99244 QuartzCore`CA::Layer::layout_if_needed(CA::Transaction*) + 380
frame #16: 0x00a990b0 QuartzCore`CA::Layer::layout_and_display_if_needed(CA::Transaction*) + 26
frame #17: 0x009ff7fa QuartzCore`CA::Context::commit_transaction(CA::Transaction*) + 294
frame #18: 0x00a00b85 QuartzCore`CA::Transaction::commit() + 393
frame #19: 0x00a01258 QuartzCore`CA::Transaction::observer_callback(__CFRunLoopObserver*, unsigned long, void*) + 92
frame #20: 0x02bff36e CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 30
frame #21: 0x02bff2bf CoreFoundation`__CFRunLoopDoObservers + 399
frame #22: 0x02bdd254 CoreFoundation`__CFRunLoopRun + 1076
frame #23: 0x02bdc9d3 CoreFoundation`CFRunLoopRunSpecific + 467
frame #24: 0x02bdc7eb CoreFoundation`CFRunLoopRunInMode + 123
frame #25: 0x036135ee GraphicsServices`GSEventRunModal + 192
frame #26: 0x0361342b GraphicsServices`GSEventRun + 104
frame #27: 0x01676f9b UIKit`UIApplicationMain + 1225
frame #28: 0x000144cd `main(argc=1, argv=0xbfffee04) + 141 at main.m:16

不知道造成这种情况的原因。

帮助将不胜感激。

由于

1 个答案:

答案 0 :(得分:1)

我强烈怀疑这......

cell.textLabel.text = [self.searchResults objectAtIndex:indexPath.row];

...正在尝试将PFUser分配给文本字符串。特定错误可能是因为单元格标签想要复制其指定的值,同样,PFUser不是为支持它而设计的。

在进行作业之前,请尝试NSLog(@"Object in search: %@", [self.searchResults objectAtIndex:indexPath.row]);进行验证。

编辑:

尝试替换......

cell.textLabel.text = [self.searchResults objectAtIndex:indexPath.row];

...与...

PFUser *user = [self.searchResults objectAtIndex:indexPath.row];
cell.textLabel.text = user.username;