2013-01-23 77 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

感謝您的意見 –