2014-09-24 21 views
0
在故事板

添加的搜索欄上的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]; 
} 

    } 

編輯:

當我輸入BT在LLDB提示這是我得到

* 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 

不知道是什麼原因造成的。

幫助將不勝感激。

感謝

+0

可以粘貼從崩潰的堆棧跟蹤到你的問題? – 2014-09-24 20:56:14

+0

對不起,我忘了補充一點。剛剛更新錯誤。 – user1120133 2014-09-24 21:06:24

+0

這很好 - 它表明有些東西試圖複製PFUser,它不能被複制 - 但調用堆棧會更好。 (創建一個異常斷點,當出現錯誤時,在調試控制檯的lldb提示符處輸入'bt'。) – 2014-09-24 21:10:40

回答

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; 
+0

正在獲取[PFUser copyWithZone:]:發送了無法識別的選擇器例如0xac70260 – user1120133 2014-09-24 21:47:42

+0

所以這是一個PFUser。爲什麼不使用'else'部分中的相同類型的代碼:分配給一個對象,然後從該對象中獲取'username'? – 2014-09-24 22:01:27

+0

我做了PFUser * user = [self.allUsers objectAtIndex:indexPath.row]; NSString * username = [user objectForKey:user.username]; cell.textLabel.text = username; NSLog(@「搜索中的對象:%@」,[self.searchResults objectAtIndex:indexPath.row]);它會在搜索中顯示此對象: { email =「[email protected]」; friendsRelation =「(<00000000>。(null) - > _User)」; username = jkl; – user1120133 2014-09-24 22:14:44