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

Uitableview TableView中的搜索栏:当键入搜索文本字符串的最后一个字符时,应用程序崩溃,uitableview,ios7,xcode5,uisearchbar,Uitableview,Ios7,Xcode5,Uisearchbar,在tableview应用程序顶部的故事板中添加的搜索栏在键入搜索文本字符串的最后一个字符时崩溃,并出现错误 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:'-[PFUser copyWithZone:]:发送到实例的选择器无法识别 - (void)viewDidLoad { [super viewDidLoad]; PFQuery *query = [PFUser query]; [query orderByAscending:@"usernam

在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
我不知道是什么原因造成的

我们将不胜感激


谢谢

我强烈怀疑

cell.textLabel.text = [self.searchResults objectAtIndex:indexPath.row];
…正在尝试将PFUser分配给文本字符串。具体的错误可能是因为单元格标签想要复制其指定的值,而且PFUser的设计也不支持这一点

请尝试
NSLog(@“搜索中的对象:%@,[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;

你能将崩溃的堆栈跟踪粘贴到你的问题中吗?对不起,我忘了添加那个。只是用错误更新了它。这很好——它表明有东西试图复制PFUser,但无法复制——但是调用堆栈会更好。(创建一个异常断点,当您收到错误时,在调试控制台的lldb提示符处键入“bt”。)我用异常断点得到的错误更新了post获取[PFUser copyWithZone:]:无法识别的选择器发送到实例0xAC70260,因此它是PFUser。为什么不使用与
else
部分相同的代码:分配给对象,然后从该对象获取
username
;NSString*username=[user objectForKey:user.username];cell.textlab.text=用户名;NSLog(@“搜索中的对象:%@,[self.searchResults objectAtIndex:indexPath.row]);它在搜索中显示此对象:{email=”jkl@gmail.com“friendsRelation=“((null)->(u User)”;username=jkl;它现在没有崩溃,但没有在tableview中加载搜索结果。为什么我还缺少其他东西?我将感谢您的帮助。