UITableViewにUISearchDisplayControllerがあります。UITableViewはサーバーからJSONデータを取得して分解し、オブジェクトクラスがそのデータを取得してオブジェクトを作成します。オブジェクトをMutable配列に追加して、テーブルのデータソースとして使用します。
'filtered'オブジェクトとして使用される別のMutable配列もあります。
それはそのように宣言されています:
self.filteredItems = [NSMutableArray arrayWithCapacity:[self.items count]];
テーブルを検索しようとすると、次のエラーが発生します。
キャッチされなかった例外'NSRangeException'が原因でアプリを終了しています。理由:' * -[__ NSArrayM objectAtIndex:]:インデックス2が境界を超えています[0 .. 1] '
これは、フィルタリングを行うために使用しているコードです。これは、一連のチュートリアルを行った後に見つかりました。return関数がNSArrayを返すように見えるため、NSPredicateを使用できません。また、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;
}
通常の表には、詳細ビューと同様に問題なく表示されます。