2013-01-23 76 views
0

我有一个方法,这给我一个错误的这一部分。 这是一个字过滤器,用户键入a,所有字出现,等等...NSMutableArray越界错误

错误是当用户删除搜索栏文本,并点击表上的东西,我超越了界限异常。

打印出来的filteredListContent是一个单独的“”条目,我应该如何保持崩溃程序?

感谢

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    if ([_filteredListContent count]>0){ 
     NSLog(@"%i",indexPath.row); 
     NSLog(@"%@",_filteredListContent); 
     _searchBar.text=[self.filteredListContent objectAtIndex:indexPath.row]; 
     //Save to user default 
+0

当用户删除搜索文本,你干了什么? –

+0

错误发生在哪一行? – rdelmar

+0

第4行_searchbar.text ... –

回答

2

我不知道是什么原因造成的问题,但根据给定的代码,它看起来像选择的行是“出界”。我会改变我的检查:

[_filteredListContent count] > 0 

喜欢的东西:

[_filteredListContent count] > indexPath.row 

同样,这仅仅是基于给定的代码。

+0

我知道这是愚蠢的...从来没有抓到它,谢谢。这工作 –

+0

@愿意 - 很高兴它的工作! – nalyd88

1

我猜你是使用搜索显示控制器在表查找。但同时你对两个表都使用相同的数组引用(self.filteredListContent)(一个是搜索结果表&,另一个是用于显示所有单词的原始表)。在搜索栏中键入字符时,您正在修改self.filteredListContent数组。这就是为什么当你搜索self.filteredListContent被修改的时候(可能会包含比用来加载原始表的对象更少的对象),然后你选择原始表的任何一行,如果你尝试访问数组中不存在的索引处的对象,它将会崩溃。

所以我建议你保留两个数组。一个用于加载原始表&一个搜索结果表

-1

以下是单词过滤的简单示例。

-(void)viewDidLoad 
{ 
    self.listOfTemArray = [[NSMutableArray alloc] init]; 
    self.ItemOfMainArray = [[NSMutableArray alloc] initWithObject:@"/ArrayList/"]; 

    [self.listOfTemArray addObjectsFromArray:self.ItemOfMainArray]; 
} 


#pragma mark - 
#pragma mark Table view Methods 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)aTableView { 
    // Return the number of sections. 
    return 1; 
} 
- (NSInteger)tableView:(UITableView *)aTableView numberOfRowsInSection:(NSInteger)section { 
    // Return the number of rows in the section. 
    return [self.listOfTemArray count]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Foobar"]; 
    if (cell == nil) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Foobar"]; 

     cell.selectionStyle = UITableViewCellSelectionStyleBlue; 
     cell.textLabel.textColor = [UIColor blackColor]; 
    } 

     cell.textLabel.text = [self.listOfTemArray objectAtIndex: indexPath.row]; 

    return cell; 
} 

#pragma mark - 
#pragma mark SearchBar methods 

- (void)searchBar:(UISearchBar *)theSearchBar textDidChange:(NSString *)searchText 
{ 
     NSString *name = @""; 
     NSString *firstLetter = @""; 

    if (self.listOfTemArray.count > 0) 
     [self.listOfTemArray removeAllObjects]; 

     if ([searchText length] > 0) 
     { 
       for (int i = 0; i < [self.ItemOfMainArray count] ; i = i+1) 
       { 
         name = [self.ItemOfMainArray objectAtIndex:i]; 

         if (name.length >= searchText.length) 
         { 
           firstLetter = [name substringWithRange:NSMakeRange(0, [searchText length])]; 
           //NSLog(@"%@",firstLetter); 

           if([firstLetter caseInsensitiveCompare:searchText] == NSOrderedSame) 
           { 
            // strings are equal except for possibly case 
            [self.listOfTemArray addObject: [self.ItemOfMainArray objectAtIndex:i]]; 
            NSLog(@"=========> %@",self.listOfTemArray); 
           } 
         } 
       } 
     } 
     else 
     { 
      [self.listOfTemArray addObjectsFromArray:self.ItemOfMainArray ]; 
     } 

     [self.tblView reloadData]; 
} 

此代码可能会在你的情况下非常有用...

谢谢:)

+0

感谢您的意见 –