我在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;
}
正常表顯示就好了,以及他們的具體意見。
您可以發佈數據源方法嗎?他們可能會出錯。 numberOfRows:可能會返回實際的對象數,而不是過濾的對象數。 – nkongara 2013-03-17 16:55:54
「我不能使用NSPredicate,因爲它似乎返回函數返回一個NSArray,並且我有一個NSMutableArray,我無法更改,因爲我沒有靜態聲明對象」 - 這句話沒有任何意義。在任何情況下,您都可以輕鬆地將NSArray轉換爲NSMutableArray,且無需額外開銷。 – matt 2013-03-17 17:26:45