2013-03-17 16 views
0

我在UITableView上有一個UISearchDisplayController。 UITableView從服務器獲取JSON數據,將其分解,然後對象類將獲取該數據並創建對象。它將對象添加到可變數組中,然後用作表的數據源。使用UISearchDisplayController時出現數組越界錯誤

還有另一個可變數組用作'過濾'對象。

它宣稱,像這樣:

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

當我嘗試搜索表我得到這個錯誤:

Terminating app due to uncaught exception 'NSRangeException', reason: '* -[__NSArrayM objectAtIndex:]: index 2 beyond bounds [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;  
} 

正常表顯示就好了,以及他們的具體意見。

+0

您可以發佈數據源方法嗎?他們可能會出錯。 numberOfRows:可能會返回實際的對象數,而不是過濾的對象數。 – nkongara 2013-03-17 16:55:54

+0

「我不能使用NSPredicate,因爲它似乎返回函數返回一個NSArray,並且我有一個NSMutableArray,我無法更改,因爲我沒有靜態聲明對象」 - 這句話沒有任何意義。在任何情況下,您都可以輕鬆地將NSArray轉換爲NSMutableArray,且無需額外開銷。 – matt 2013-03-17 17:26:45

回答

1

在您的委託方法你必須在「numberOfRows」:

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

然後在「的cellForRowAtIndexPath」:

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

你確定的行數,然後比較其不同的表數組從中獲取數據。

+0

優秀!這修復了崩潰,但現在無論我輸入什麼,它都會返回搜索的所有行。什麼可能導致? – Anthony 2013-03-17 17:22:23

+0

太棒了!您的'updatesTableView'變量設置是否正確(出口在界面構建器中設置),還是在viewDidLoad中分配? – miwic 2013-03-17 17:30:06

+0

該表在加載時正確加載數據,然後您點擊搜索欄並激活顯示控制器,當您開始鍵入它時,只是將所有行都返回到searchdisplaycontroller tableView中。因爲我認爲filterContentForSearchText工作不正常。但它對我來說看起來不錯,但我從來沒有這樣做過。 – Anthony 2013-03-17 17:32:03

1
- (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]; 
    } 
} 
+0

將numberOfRowsInSection方法替換爲此方法 – Pradeep 2013-03-18 06:00:46