Вы можете попробовать 2 вещи.
- Заполните UITableView словарем вместо массива. Здесь вы можете использовать словарь ключ и значение в ячейке.
- Вы можете создать класс, скажем, Workout, с двумя свойствами - NSString * упражнение и NSString * мышца.
Для 2-го подхода вам придется изменить свой код как
- (void) searchTableView {
NSString *searchText = searchBar.text;
NSMutableArray *searchArray = [[NSMutableArray alloc] init];
for (NSDictionary *dictionary in self.exerciseArray)
{
NSString *value = [dictionary objectForKey:@"exerciseName"];
//workout alloc init
workout.exercise = [dictionary objectForKey:@"exerciseName"];
workout.muscle = [dictionary objectForKey:@"muscleName"];
[searchArray addObject:workout];
}
/*See if this is necessary to implement*/
//for (NSString *sTemp in searchArray)
//{
// NSRange titleResultsRange = [sTemp rangeOfString:searchText options:NSCaseInsensitiveSearch];
//if (titleResultsRange.length > 0)
//[listOfItems addObject:sTemp];
//}
//searchArray = nil;
}
Я бы посоветовал вам выбрать первый подход, который довольно прост.
Если вы выберете 1-й подход, вы можете попробовать это.
- (void) searchTableView {
NSString *searchText = searchBar.text;
for (NSDictionary *dictionary in self.exerciseArray)
{
NSString *exerciseName = [dictionary objectForKey:@"exerciseName"];
NSRange titleResultsRange = [exerciseName rangeOfString:searchText options:NSCaseInsensitiveSearch];
if (titleResultsRange.length > 0)
[listOfItems addObject:dictionary];
}
}
- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section {
return [listOfItems count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *stringIdentifier = @"CellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:stringIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:stringIdentifier];
}
NSDictionary *dictionary = (NSDictionary *)[listOfItems objectAtIndex:indexPath.row];
[cell.textLabel setText:[dictionary objectForKey:@"exerciseName"]];
[cell.detailTextLabel setText:[dictionary objectForKey:@"muscleName"]];
return cell;
}