私はテーブルビュー用の検索バーを設定しました。私は両方のtextLabels (メインテキスト)、およびdetailTextLabels(基本テキスト)を使用して、検索を絞り込む際にメインラベルと詳細ラベルの関係を維持します。今、主なラベルだけを検索することはできますが、詳細ラベルは関連する主要なラベルには残りません。私は主なラベルと詳細ラベルの文字列を運ぶ2つの配列を持っています。ここにいくつかのコードがあります。セクションのUITableViewの検索バー:textLabelsとdetailTextLabels(iOS 5.0、xcode 4.2)の両方を検索したいです。
メインラベル(groceryStores)を有する配列、詳細ラベル(groceryStoreAddresses)の初期化、ならびに検索アレイ
- (void)viewDidLoad
{
[super viewDidLoad];
self.groceryStores = [NSArray arrayWithObjects:@"Safeway",@"Grocery Store 1",@"Grocery Store 2",@"Grocery Store 3",@"Grocery Store 4",@"Grocery Store 5",@"Grocery Store 6",@"Grocery Store 7",nil];
self.groceryStoreAddresses = [NSArray arrayWithObjects:@"1444 Shattuck Place, Berkeley, CA 94709",@"Address 1",@"Address 2",@"Address 3",@"Address 4",@"Address 5",@"Address 6",@"Address 7",nil];
self.searchGroceryStores = [NSMutableArray arrayWithCapacity:[self.groceryStores count]];
}
検索方法
- (BOOL) searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
[self.searchGroceryStores removeAllObjects] ;
for (NSString *groceryStore in self.groceryStores) {
NSRange result = [groceryStore rangeOfString:searchString options:
NSCaseInsensitiveSearch];
if (result.location != NSNotFound)
[self.searchGroceryStores addObject:groceryStore];
}
return YES;
}
行検索機能を調整しました
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView == self.searchDisplayController.searchResultsTableView) {
return [self.searchGroceryStores count];
}
else {
return [self.groceryStores count];
}
}
cellForRowAtIndexPath検索機能を調整する
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"UITableViewCellAccessoryDisclosureIndicator"];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
if (tableView == self.searchDisplayController.searchResultsTableView) {
cell.textLabel.text = [self.searchGroceryStores objectAtIndex:indexPath.row];
cell.detailTextLabel.text = [self.groceryStoreAddresses objectAtIndex:indexPath.row];
}
else {
cell.textLabel.text = [self.groceryStores objectAtIndex:indexPath.row];
cell.detailTextLabel.text = [self.groceryStoreAddresses objectAtIndex:indexPath.row];
}
// Configure the cell...
return cell;
}
できるだけお手伝いをしてください。あなたのお時間をありがとう、ありがとうございました。
私はsearchGroceryStoreAddresses配列を作成しましたが、これをsearchDisplayControllerメソッドに実装するにはどうすればよいですか? &&または||を追加しようとするたびに文をforループ引数に渡すと、xcodeはエラーになります。 – user1072337
私はまだ2つの配列を互いに並列に保つ方法を見つけるのが難しいです。両方の配列の同じインデックスにあるオブジェクトが互いに同期されるようにして、正しい詳細ラベルが対応するテキストラベルとともに表示されるようにするにはどうすればよいですか? – user1072337
常に両方のアレイを更新するようにしてください。 –