NSMutableArray 仅在填充另一个数组时越界

NSMutableArray out of bounds only when populated with another array

所以我有一个 UItableView,它会根据用户输入自动填充单元格。我有一个名为 arrColors 的颜色名称主数组,以及一个名为 arrAutoCorrect 的 'temp' 数组,它会在用户键入时填充。

两个数组都是 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

附加信息: 如果我注释掉添加子字符串长度为 0 的所有颜色 searchAutocompleteEntriesWithSubstring,则应用程序不会崩溃。

NSLOG 统计

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
}

一定要阅读你的文档,孩子们。

我错过了方法:

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

即使我使用的是静态单元(不需要 tableView:heightForRowAtIndexPath ),动态 table 也需要。