繁体   English   中英

使用UISearchDisplayController时,数组超出范围错误

[英]Array out of bounds error when using UISearchDisplayController

我在UITableView上有一个UISearchDisplayController。 UITableView从服务器获取JSON数据,将其分解,并且对象类获取该数据并创建对象。 它将Objects添加到Mutable数组,然后用作表的数据源。

还有另一个Mutable数组用作'过滤'对象。

它的声明如下:

self.filteredItems = [NSMutableArray arrayWithCapacity:[self.items count]];

当我尝试搜索表时,我收到此错误:

因未捕获的异常'NSRangeException'而终止应用程序,原因:' * - [__ NSArrayM objectAtIndex:]:索引2超出边界[0 .. 1]'

这是我用来做过滤的代码,我在经过一堆教程后发现,我不能使用NSPredicate,因为看起来返回函数返回NSArray,我有一个NSMutableArray,我不能改变,因为我没有静态地声明对象。

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{    
    /*
     Update the filtered array based on the search text and scope.
    */

    [self.filteredItems removeAllObjects];

    for (Update *update in items)
    {        
        if ([scope isEqualToString:@"Serial Number"]) {
            NSComparisonResult result = [[update Serial_Number] compare:searchText options:NSCaseInsensitiveSearch range:NSMakeRange(0, [searchText length])];
            if (result == NSOrderedSame){
                [filteredItems addObject:update];
            }
        } else if ([scope isEqualToString:@"Ticket Number"]) {
            NSComparisonResult result = [[update Ticket_Number] compare:searchText options:NSCaseInsensitiveSearch range:NSMakeRange(0, [searchText length])];
            if (result == NSOrderedSame){
                [filteredItems addObject:update];
            }
        } else if ([scope isEqualToString:@"Customer"]) {
            NSComparisonResult result = [[update Customer] compare:searchText options:NSCaseInsensitiveSearch range:NSMakeRange(0, [searchText length])];
            if (result == NSOrderedSame){
                [filteredItems addObject:update];
            }
        }
    }
}

不太确定我做错了什么,我以前从未试图实现过这个控件。

我实际上运行了断点,我可以看到对象被添加到已过滤的数组中,但突然它崩溃了。

如果您需要代码的任何其他部分来帮助我,请告诉我。

谢谢!

编辑:

这是委托方法:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    if (updatesTableView == self.searchDisplayController.searchResultsTableView) {
        return [self.filteredItems count];
    } else  {
        return [self.items count];
    }
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }

    Update *update = nil;

    if (tableView == self.searchDisplayController.searchResultsTableView) {
        update = [filteredItems objectAtIndex:indexPath.row];
    } else {
        // Configure the cell...
        update = [items objectAtIndex:indexPath.row];
    }

    [[cell textLabel] setText:[update Serial_Number]];
    [[cell detailTextLabel] setText:[NSString stringWithFormat:@"Customer: %@", [update Customer]]];

    cell.textLabel.adjustsFontSizeToFitWidth = YES;
    cell.detailTextLabel.adjustsFontSizeToFitWidth = YES;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;    
}

普通表显示正常以及它们的详细视图。

在你的委托方法中你有'numberOfRows':

if (updatesTableView == self.searchDisplayController.searchResultsTableView) {

然后在'cellForRowAtIndexPath'中:

if (tableView == self.searchDisplayController.searchResultsTableView) {

您正在比较不同的表以确定行数,然后比较哪个数组来获取数据。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    if (tableView == self.searchDisplayController.searchResultsTableView) {
        return [self.filteredItems count];
    } else  {
        return [self.items count];
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM