uisearchbar uisearchdisplaycontroller Табличная ошибка субтитров
У меня небольшая проблема с панелью поиска для таблицы
Я сделал табличное представление с массивом tableViewArray.
TableViewArray состоит из множества строк другого массива, состоящего из [text, distance]. Все работает нормально.
Теперь я добавил searchBar и searchdisplaycontroller, который осуществлял поиск на основе нового массива строк (из объекта "text" tableViewArray). Я думал, что поиск должен быть доступен только для текста, и метод поиска реализован на этом.
Теперь, когда я получаю результат поиска, он выглядит хорошо, и поиск возвращает ожидаемые строки. Проблема с подзаголовком поиска tableViews. Он показывает расстояния для строк 1, 2, 3 для tableViewArray.
Мне нужно, чтобы отобразить расстояние до текста, отображаемого в строках таблицы поиска. Я полагаю, мне нужно создать новый массив табличного представления для результатов поиска, состоящий из [расстояние текста]. текст не проблема, так как он из результатов поиска, но как мне сопоставить новое расстояние со старым расстоянием???
Метод поиска, который я использую в поисковике:
searchResults = [[NSArray alloc]init];
NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"SELF contains[cd] %@",
searchText];
searchResults = [searchItems filteredArrayUsingPredicate:resultPredicate];
Надеюсь, кто-нибудь может помочь:) Заранее спасибо!
Оригинальный код:
- (NSInteger)tableView:(UITableView *)theTableView numberOfRowsInSection:(NSInteger)section
{
if (theTableView == self.searchDisplayController.searchResultsTableView) {
return [searchResults count];
} else {
return [tableViewArray count];
}
}
- (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleSubtitle
reuseIdentifier: CellIdentifier] ;
if (theTableView == self.searchDisplayController.searchResultsTableView) {
} else {
cell = [theTableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
}
// Configure the cell...
// cell.textLabel.numberOfLines = 0;
// cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
if (theTableView == self.searchDisplayController.searchResultsTableView) {
cell.textLabel.text = [searchResults objectAtIndex:indexPath.row];
} else {
cell.textLabel.text = [[[tableViewArray objectAtIndex:indexPath.row] objectAtIndex:0] subtitle];
}
cell.detailTextLabel.textColor = [UIColor redColor];
float blabla= [[[tableViewArray objectAtIndex:indexPath.row] objectAtIndex: 1] doubleValue];
if (blabla < 1000) {
cell.detailTextLabel.text = [NSString stringWithFormat:@"%.2f m",blabla];
} else {
cell.detailTextLabel.text = [NSString stringWithFormat:@"%.2f km",blabla/1000];
}
NSString *text = [[[tableViewArray objectAtIndex:indexPath.row] objectAtIndex:0] subtitle];
NSRange q8RangeValue = [text rangeOfString:@"Q8" options:NSCaseInsensitiveSearch];
NSRange okRangeValue = [text rangeOfString:@"OK" options:NSCaseInsensitiveSearch];
if (q8RangeValue.length >0 ) {
cell.imageView.image = [UIImage imageNamed:@"q8.png"];
} else if (okRangeValue.length >0 ) {
cell.imageView.image = [UIImage imageNamed:@"OK logo.png"];
} else {
cell.imageView.image = nil;
}
return cell;
}
И где я делаю массив для поиска:
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
NSLog(@"didUpdateToLocation Location calculator distance array for the tableView");
NSMutableArray * distancesInReverseOrder = [[NSMutableArray alloc] init];
for (int i = 0; i<allAnnotations.count; i++) {
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0] ;
CLLocationCoordinate2D annotationCoord = [[allAnnotations objectAtIndex:indexPath.row] coordinate];
CLLocation *location = [[CLLocation alloc] initWithLatitude:annotationCoord.latitude longitude:annotationCoord.longitude];
distanceToMe = [newLocation distanceFromLocation:location];
[distancesInReverseOrder insertObject:[NSNumber numberWithFloat: distanceToMe] atIndex:0];
}
distances = [[NSMutableArray alloc] initWithArray:[[distancesInReverseOrder reverseObjectEnumerator] allObjects]];
// Assuming you have your points on the map in an NSArray called
// allAnnotations and your distances in distances, create a
// new mutable array to hold both
tableViewArray = [[NSMutableArray alloc] init];
// Iterate over all of the points, and add a new element to the mutable
// array which is a new array containing a point and its distance
for (int i = 0; i < allAnnotations.count; i++) {
NSArray *newItem = [NSArray arrayWithObjects: [allAnnotations objectAtIndex: i], [distances objectAtIndex: i], nil];
[tableViewArray addObject: newItem];
}
// Now, sort the new array based upon the distance in the second element
// of each array (ie, the distance).
[tableViewArray sortUsingComparator: ^(id obj1, id obj2) {
NSNumber *dist1 = [obj1 objectAtIndex:1];
NSNumber *dist2 = [obj2 objectAtIndex:1];
return [dist1 compare:dist2];
}];
searchResults = [NSMutableArray arrayWithCapacity:[tableViewArray count]];
searchItems = [[NSMutableArray alloc] init];
for (int i = 0; i < allAnnotations.count; i++) {
NSArray *newItem = [NSArray arrayWithObjects: [[[tableViewArray objectAtIndex:i] objectAtIndex:0] subtitle], @"bla", nil];
[searchItems addObject: newItem];
}
/*
for (int i=0; i<tableViewArray.count; i++) {
[searchItems insertObject:[[[tableViewArray objectAtIndex:i] objectAtIndex:0] subtitle] atIndex:0];
}
*/
NSLog(@"searchitems count is %i", searchItems.count);
[tableView reloadData];
[locationManager stopUpdatingLocation];
}
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
/*
// Update the filtered array based on the search text and scope.
// Remove all objects from the filtered search array
[searchResults removeAllObjects];
// Filter the array using NSPredicate
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.name contains[c] %@",searchText];
// searchResults = [NSMutableArray arrayWithArray: [[tableViewArray objectAtIndex: 1] FilterArrayUsingPredicate: предикат]];
* /
/*
searchResults = [[NSArray alloc] init];
NSPredicate *resultPredicate = [NSPredicate
predicateWithFormat:@"SELF contains[cd] %@",
searchText];
searchResults =[searchItems filteredArrayUsingPredicate:resultPredicate];
*/
// Create index set of all objects in textArray that contain searchText:
NSIndexSet *set = [searchItems indexesOfObjectsPassingTest:
^BOOL(NSString *text, NSUInteger idx, BOOL *stop) {
NSRange range = [text rangeOfString:searchText options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch)];
return (range.location != NSNotFound);
}];
// Filter textArray:
filteredTextArray = [searchItems objectsAtIndexes:set];
// Filter distanceArray:
filteredDistanceArray = [distances objectsAtIndexes:set];
NSLog(@"filtered text array is %@", filteredTextArray);
NSLog(@"filtered distance array is %@",filteredDistanceArray);
1 ответ
Если я правильно понимаю вашу проблему, у вас есть 2 отдельных массива, которые используются в качестве источника данных для табличного представления, давайте назовем их textArray
а также distanceArray
,
Теперь вы фильтруете textArray
в соответствии со строкой поиска, и вам нужна "соответствующая" фильтрация distanceArray
,
Один из способов сделать это - заменить filteredArrayUsingPredicate
с indexesOfObjectsPassingTest
потому что это возвращает набор совпадающих индексов, которые могут быть применены к обоим массивам:
// Create index set of all objects in textArray that contain searchText:
NSIndexSet *set = [textArray indexesOfObjectsPassingTest:
^BOOL(NSString *text, NSUInteger idx, BOOL *stop) {
NSRange range = [text rangeOfString:searchText options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch)];
return (range.location != NSNotFound);
}];
// Filter textArray:
filteredTextArray = [textArray objectsAtIndexes:set];
// Filter distanceArray:
filteredDistanceArray = [distanceArray objectsAtIndexes:set];
Теперь вы можете использовать filteredTextArray
а также filteredDistanceArray
в качестве источника данных для таблицы поиска.
В качестве альтернативы вы можете использовать один массив в качестве источника данных, если каждый объект в массиве является, например, словарем, содержащим как текст, так и расстояние для одной строки.
ОБНОВЛЕНИЕ: Как я понимаю сейчас, каждый элемент вашего tableViewArray
массив из 2 элементов (один для текста и один для расстояния).
В этом случае я бы рекомендовал отфильтровать tableViewArray
непосредственно:
NSIndexSet *set = [tableViewArray indexesOfObjectsPassingTest:
^BOOL(NSArray *item, NSUInteger idx, BOOL *stop) {
NSString *subtitle = [[item objectAtIndex:0] subtitle];
NSRange range = [subtitle rangeOfString:searchText options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch)];
return (range.location != NSNotFound);
}];
searchResults = [tableViewArray objectsAtIndexes:set];
Сейчас searchResults
фильтрованный массив, и каждый элемент имеет ту же структуру, что и элементы в tableViewArray
,
Это упрощает вещи в cellForRowAtIndexPath
например,
if (theTableView == self.searchDisplayController.searchResultsTableView) {
cell.textLabel.text = [[[searchResults objectAtIndex:indexPath.row] objectAtIndex:0] subtitle];
distance = [[[searchResults objectAtIndex:indexPath.row] objectAtIndex: 1] doubleValue];
} else {
cell.textLabel.text = [[[tableViewArray objectAtIndex:indexPath.row] objectAtIndex:0] subtitle];
distance = [[[tableViewArray objectAtIndex:indexPath.row] objectAtIndex: 1] doubleValue];
}