Added search bar in the storyboard on top of tableview app crashes when type last character of search text string with the error
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[PFUser copyWithZone:]: unrecognized selector sent to instance
- (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];
}
}
EDIT:
When i type bt at lldb prompt this is what i get
* 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
Have no idea what is causing this.
Help will be appreciated.
Thanks
I strongly suspect that this...
cell.textLabel.text = [self.searchResults objectAtIndex:indexPath.row];
...is trying to assign a PFUser to a text string. The specific error is probably because the cell label wants to make a copy of its assigned value and, again, PFUser isn't designed to support that.
Try NSLog(@"Object in search: %@", [self.searchResults objectAtIndex:indexPath.row]);
to verify that before you make the assignment.
Edit:
Try replacing...
cell.textLabel.text = [self.searchResults objectAtIndex:indexPath.row];
...with...
PFUser *user = [self.searchResults objectAtIndex:indexPath.row];
cell.textLabel.text = user.username;