2014-10-28 45 views
0

我收到偶爾崩潰當我嘗試使用UISearchBar/searchDisplayController搜索某些文本時。我正在通過JSON從在線數據庫中搜索數據,並且99%的時間內一切正常。我正在使用textDidChange委託方法在其搜索中搜索用戶類型。iOS在線搜索數據庫時遇到NSRangeException崩潰

這裏是崩潰我得到的一個例子:

Fatal Exception: NSRangeException 
-[__NSCFArray objectAtIndex:]: index (2) beyond bounds (2) 

Fatal Exception: NSRangeException 
*** -[__NSArrayI objectAtIndex:]: index 6 beyond bounds [0 .. 0] 

這真的很難讓我明確了什麼是給我的崩潰,因爲當我嘗試重新什麼造成了事故,我不能。該應用程序可以很好地工作幾天,並進行大量「測試」(例如,我一遍又一遍地輸入查詢)。然後隨機會崩潰,然後幾天沒有。

以下是一些更新我的TableViews的委託方法。

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
// Return the number of sections. 
return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
// Return the number of rows in the section. 
return [[SearchInfo sharedInfo].searchArray count]; 
} 

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

////////Adds search data to local Array to pass it around the applicatio////// 
searchData = [[SearchInfo sharedInfo].searchArray objectAtIndex:indexPath.row]; 
static NSString *cellIdentifier = @"Cell"; 

//Standard Table View Cell 
UITableViewCell *searchCell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 
if (searchCell==nil) { 

searchCell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier]; 
} 


///////////Search bar tableview////////// 
if (tableView == self.searchDisplayController.searchResultsTableView) { 
searchCell.textLabel.text = [SearchData objectForKey:@"title"]; 
searchCell.detailTextLabel.text = [SearchData objectForKey:@"system_title"]; 
} 

///////Builds the custom tableview cell////// 
UILabel *searchTitleLabel = (UILabel*)[searchCell viewWithTag:1]; 
searchTitleLabel.text = [searchData objectForKey:@"title"]; 
UILabel *systemTitleLabel = (UILabel *)[searchCell viewWithTag:2]; 
systemTitleLabel.text = [searchData objectForKey:@"system_title"]; 
UILabel *devLabel = (UILabel*)[searchDataCell viewWithTag:3]; 

if([[searchData objectForKey:@"developer"] isKindOfClass:[NSString class]]) 

{ 
if([[searchData objectForKey:@"developer"] isEqualToString:@""] || [[searchData objectForKey:@"developer"] isEqualToString:@"<null>"]) 
{ 
developerLabel.text = @"Unknown"; 
} 
else { 
developerLabel.text = [NSString stringWithFormat:@"%@",[searchData objectForKey:@"developer"]]; 
} 

} 
else { 
developerLabel.text = @"Unknown"; 
} 



//////////////////////////////////////////////////////////////// 
return searchCell; 
} 

如果您還有其他問題,請告訴我!我在這裏拉我的頭髮。

這裏是崩潰日誌:

Thread : Fatal Exception: NSRangeException 
0 CoreFoundation     0x2577cc1f __exceptionPreprocess + 126 
1 libobjc.A.dylib    0x32f27c8b objc_exception_throw + 38 
2 CoreFoundation     0x25691a8d CFRunLoopRemoveTimer 
3 Archive.vg      0x00073ddb -[CenterViewController tableView:cellForRowAtIndexPath:] (CenterViewController.m:192) 
4 UIKit       0x28f018c7 -[UITableView _createPreparedCellForGlobalRow:withIndexPath:willDisplay:] + 474 
5 UIKit       0x28f0198b -[UITableView _createPreparedCellForGlobalRow:willDisplay:] + 54 
6 UIKit       0x28ef70e1 -[UITableView _updateVisibleCellsNow:isRecursive:] + 2268 
7 UIKit       0x28d0f2ff -[UITableView layoutSubviews] + 186 
8 UIKit       0x28c394d7 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 514 
9 QuartzCore      0x28661a0d -[CALayer layoutSublayers] + 136 
10 QuartzCore      0x2865d3e5 CA::Layer::layout_if_needed(CA::Transaction*) + 360 
11 QuartzCore      0x2865d26d CA::Layer::layout_and_display_if_needed(CA::Transaction*) + 16 
12 QuartzCore      0x2865cc51 CA::Context::commit_transaction(CA::Transaction*) + 224 
13 QuartzCore      0x2865ca55 CA::Transaction::commit() + 324 
14 QuartzCore      0x2865692d CA::Transaction::observer_callback(__CFRunLoopObserver*, unsigned long, void*) + 56 
15 CoreFoundation     0x257433b5 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 20 
16 CoreFoundation     0x25740a73 __CFRunLoopDoObservers + 278 
17 CoreFoundation     0x25740e7b __CFRunLoopRun + 914 
18 CoreFoundation     0x2568f211 CFRunLoopRunSpecific + 476 
19 CoreFoundation     0x2568f023 CFRunLoopRunInMode + 106 
20 GraphicsServices    0x2ca880a9 GSEventRunModal + 136 
21 UIKit       0x28c9b1d1 UIApplicationMain + 1440 
22 Archive.vg      0x0009d0cd main (main.m:16) 
23 libdyld.dylib     0x334a7aaf start + 2 
+0

沒關係所有的代碼。只需顯示崩潰發生的代碼_,請。 – matt 2014-10-28 20:49:53

+0

'[SearchInfo sharedInfo] .searchArray'正在發生明顯變化。您應該將其鎖定,以便在對'numberOfRowsInSection:'和'cellForRowAtIndexPath:'的調用之間不發生變化。 – 2014-10-28 20:51:34

+0

@matt我添加了一個崩潰日誌。它與SIGABRT崩潰。當我回家時,我會添加一個異常斷點。 – DookieMan 2014-10-28 20:57:20

回答

0

可以爲例外設置斷點。它在調試

真正的幫助,您可以啓用它菜單上調試 - > Breakpoints->創建異常斷點

接下來你打的異常時,你會看到它從何而來。

補充:

@synchronized([SearchInfo sharedInfo].searchArray) { ... update table here ... }

+0

當您點擊斷點時,您可以看到自己的位置,但不會顯示錯誤消息,直到您繼續(Control-Command-Y)兩次。斷點位於打印錯誤消息之前。 – zaph 2014-10-28 20:57:08

+0

嘗試鎖定或@synchronize [SearchInfo sharedInfo] .searchArray以防止表更新自己時更改它? – ekiyanov 2014-10-28 21:02:34

+0

@ekiyanov就像@synchronized(self){[SearchInfo sharedInfo] .searchArray}一樣簡單嗎? – DookieMan 2014-10-28 21:06:21

0

你說的沒錯,這變化的用戶輸入文本。這是問題的根源嗎?

是的。在加載表格時,您需要保持數據的一致性。如果您想懶惰,只需執行indexPath.row < [[SearchInfo sharedInfo].searchArray count]檢查並返回空單元格。這不是最好的解決方案。實際上,你不應該經常改變你的數據;如果您確實需要更改數據,請爲每個數據集創建一個新的UITableView,並靜態分配其數據源以確保它們獨立加載數據。

+0

好吧,我會給我一個鏡頭,當我回家。謝謝! – DookieMan 2014-10-28 21:07:21

相關問題