[英]Search Bar for UITableView: I would like it to search through both textLabels and detailTextLabels (iOS 5.0, xcode 4.2)
我已經為表格視圖設置了一個搜索欄,我希望它同時搜索textLabels(主文本)和detailTextLabels(基礎文本),同時在縮小范圍時保持主標簽和明細標簽之間的關系搜索。 現在,我只能搜索主要標簽,但詳細標簽將不會與相關的主要標簽保持一致。 我有兩個數組,分別包含主要標簽和詳細標簽的字符串。 這是一些代碼。
初始化包含主標簽(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
數組與searchGroceryStores
數組並行。 例:
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
[self.searchGroceryStores removeAllObjects];
[self.searchGroceryStoreAddresses removeAllObjects];
for (int i = 0, c = self.groceryStores.count; i < c; ++i) {
NSString *groceryStore = [self.groceryStores objectAtIndex:i];
NSString *address = [self.groceryStoreAddresses objectAtIndex:i];
NSRange result = [groceryStore rangeOfString:searchString options:
NSCaseInsensitiveSearch];
if (result.location != NSNotFound) {
[self.searchGroceryStores addObject:groceryStore];
[self.searchGroceryStoreAddresses addObject:address];
}
}
return YES;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.