2015-02-04 59 views
-1

所以我有一個UItableView,它根據用戶輸入自動填充單元格。我有一個名爲arrColors的顏色名稱的主數組,以及一個名爲arrAutoCorrect的「temp」數組,當用戶輸入時會填充該數組。只有當填充另一個數組時,NSMutableArray纔會出現界限

這兩個數組都是NSMutableArrays。

如果用戶清除所有文本,則應從所有顏色中完全加載臨時數組。 顏色數組中有21種顏色(字符串)。

我得到一個索引超出界限17(0 - 16)的錯誤。

*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 17 beyond bounds [0 .. 16]' 

這讓我瘋狂。

這17個來自哪裏?當用戶清除文本框的所有內容時,該錯誤只會再次顯示。

我NSLogged幾乎所有我可以,我找不到什麼調用這個17範圍內。

我的代碼

- (void)searchAutocompleteEntriesWithSubstring:(NSString *)substring :(NSMutableArray*) passedArray { 

    [self.arrAutoComplete removeAllObjects]; 

    if ([substring length] == 0) { 
     //add all items 
     int count = 0; 
     for (NSString* c in passedArray) { 
      NSLog(@"Count: %i", count); 
      [self.arrAutoComplete addObject:c]; 
      count++; 
     } 

    } else { 
     //Narrow down 
     for(NSString *curString in passedArray) { 
      NSRange substringRange = [curString rangeOfString:substring options:NSCaseInsensitiveSearch]; 
      NSLog(@"my range is %@", NSStringFromRange(substringRange)); 
      if (substringRange.location == 0) { 
       [self.arrAutoComplete addObject:curString]; 
      } 
     } 

    } 
    [self.autocompleteTableView reloadData]; 
} 



#pragma mark - TABLE VIEW DELEGATES 

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

     // Return the number of sections. 
     //NSLog(@"Number of sections to call for autocomplete table is: 1"); 
     return 1; 

} 

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


     // NSLog(@"Number of rows in section for autocomplete is: %lu", (long)[self.arrAutoComplete count]); 
     return [self.arrAutoComplete count]; 



} 


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    UITableViewCell *cell = nil; 
    //NSLog(@"Default cell delegate called"); 
    NSUInteger section = [indexPath section]; 
    // NSLog(@"Section: %lu", (unsigned long)section); 

    NSUInteger row = [indexPath row]; 
    NSLog(@"Row : %lu", (unsigned long)row); 


     //NSLog(@"Cell for autocomplete called"); 

     static NSString *AutoCompleteRowIdentifier = @"AutoCompleteRowIdentifier"; 
     cell = [tableView dequeueReusableCellWithIdentifier:AutoCompleteRowIdentifier]; 
     if (cell == nil) { 
      //NSLog(@"Cell for autocomplete was nil, createing new"); 
      cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:AutoCompleteRowIdentifier]; 
     } 

     //NSLog(@"Indes path row: %lu", indexPath.row); 
     cell.textLabel.text = [self.arrColors objectAtIndex:row]; 
     //NSLog(@"Cell for autocomplete text should be: %@",[self.arrColors objectAtIndex:indexPath.row]); 



    return cell; 
} 

我崩潰日誌

2015-02-04 17:47:46.512 App[4033:1312614] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 17 beyond bounds [0 .. 16]' 
*** First throw call stack: 
(
    0 CoreFoundation      0x00000001037c9f35 __exceptionPreprocess + 165 
    1 libobjc.A.dylib      0x000000010310bbb7 objc_exception_throw + 45 
    2 CoreFoundation      0x00000001036c201e -[__NSArrayI objectAtIndex:] + 190 
    3 UIKit        0x0000000104193856 -[UITableViewDataSource tableView:heightForRowAtIndexPath:] + 109 
    4 UIKit        0x0000000103ec726b __66-[UISectionRowData refreshWithSection:tableView:tableViewRowData:]_block_invoke + 302 
    5 UIKit        0x0000000103ec68fe -[UISectionRowData refreshWithSection:tableView:tableViewRowData:] + 4125 
    6 UIKit        0x0000000103eca098 -[UITableViewRowData numberOfRows] + 97 
    7 UIKit        0x0000000103d328fc -[UITableView noteNumberOfRowsChanged] + 133 
    8 UIKit        0x0000000103d3203d -[UITableView reloadData] + 1316 

附加信息: 如果我註釋掉所有的顏色searchAutocompleteEntriesWithSubstring其中子長度爲0的加入,則應用程序不會崩潰。

的NSLog STATMENTS

2015-02-04 17:54:19.155 Tops[4065:1359488] Number of sections to call for autocomplete table is: 1 
2015-02-04 17:54:19.155 Tops[4065:1359488] Number of rows in section for autocomplete is: 3 
2015-02-04 17:54:19.156 Tops[4065:1359488] Default cell delegate called 
2015-02-04 17:54:19.156 Tops[4065:1359488] Section: 0 
2015-02-04 17:54:19.156 Tops[4065:1359488] Row : 0 
2015-02-04 17:54:19.157 Tops[4065:1359488] Default cell delegate called 
2015-02-04 17:54:19.157 Tops[4065:1359488] Section: 0 
2015-02-04 17:54:19.157 Tops[4065:1359488] Row : 1 
2015-02-04 17:54:19.158 Tops[4065:1359488] Default cell delegate called 
2015-02-04 17:54:19.158 Tops[4065:1359488] Section: 0 
2015-02-04 17:54:19.158 Tops[4065:1359488] Row : 2 
2015-02-04 17:54:19.612 Tops[4065:1359488] Substring: 
2015-02-04 17:54:19.612 Tops[4065:1359488] Count: 0 
2015-02-04 17:54:19.612 Tops[4065:1359488] Count: 1 
//Removed for brevity 
2015-02-04 17:54:19.617 Tops[4065:1359488] Count: 19 
2015-02-04 17:54:19.617 Tops[4065:1359488] Count: 20 
2015-02-04 17:54:19.617 Tops[4065:1359488] Number of sections to call for autocomplete table is: 1 
2015-02-04 17:54:19.618 Tops[4065:1359488] Number of rows in section for autocomplete is: 21 
2015-02-04 17:54:19.622 Tops[4065:1359488] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 17 beyond bounds [0 .. 16]' 

更新解決

我缺少的方法:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
    return 60.0f;//some random number 
} 

請務必閱讀您的文件,小子。

+1

你可以發佈'NSLog'的輸出嗎?異常說數組中有17個對象,並且您正在嘗試訪問第18個(索引17,因爲數組從0開始)。哪一行是崩潰? – NobodyNada

+0

對於searchAutocompleteEntriesWithSubstring方法,在passedArray中有21個對象。傳入的數組是arrColors –

+1

哪條線會崩潰? – NobodyNada

回答

0

我缺少的方法:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
    return 60.0f;//some random number 
} 

即使我使用的是靜態的細胞,(這並不需要tableView:heightForRowAtIndexPath),動態表一樣。

相關問題