3

Я хочу выполнить поиск изображений с помощью search bar в tableview. Мой поиск работает, но для показа изображения я использую этот код в

- (void)viewDidLoad
{
    [super viewDidLoad];

_array18 = [[NSArray alloc ]initWithObjects:@"title1",@"title2",@"title3", nil];
_image = [[NSArray alloc ]initWithObjects:[UIImage imageNamed:@"title1.jpg"],[UIImage imageNamed:@"title2.jpg"],[UIImage imageNamed:@"title3.jpg"], nil];

}

- (void)searchForText:(NSString*)searchText scope:(NSString*)scope
{

NSPredicate *resultPredicate = [NSPredicate
                                predicateWithFormat:@"SELF contains[cd] %@",
                                searchText];

_searchResults = [_array18 filteredArrayUsingPredicate:resultPredicate];
}

Но код в cellForRowAtIndexPath не является простым решением, если у меня есть 100 изображений или больше мне нужно создавать 100 проверок. Как можно упростить этот код или есть ли более простое решение?

 (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"Cell";

cell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

if (cell == nil) {
    cell= [[cell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}

if ([[_searchResults objectAtIndex:indexPath.row]isEqualToString:@"title1"]) {
        cell.imageView.image = [_image objectAtIndex:0];
    }
    if ([[_searchResults objectAtIndex:indexPath.row]isEqualToString:@"title2"]) {
        cell.imageView.image = [_image objectAtIndex:1];
    }
    if ([[_searchResults objectAtIndex:indexPath.row]isEqualToString:@"title3"]) {
        cell.imageView.image = [_image objectAtIndex:2];
    }
}
1
  • Зачем 100 проверок писать, если можно сразу подставлять в картинку: [UIImage imageNamed[_searchResults objectAtIndex:indexPath.row]] ?
    – VAndrJ
    30 окт 2017 в 17:52

1 ответ 1

1

Используйте словарь:

@property (strong,nonatomic)NSMutableDictionary *dic;

- (void)viewDidLoad {
    [super viewDidLoad];
    _array18 = [[NSArray alloc]initWithObjects:@"title1",@"title2",@"title3", nil];
    [self initDic];
}

- (void)initDic {
    self.dic = [[NSMutableDictionary alloc] initWithDictionary:@{ @"title1":[UIImage imageNamed:@"title1.jpg"],
                                                                  @"title2":[UIImage imageNamed:@"title2.jpg"],
                                                                  @"title3":[UIImage imageNamed:@"title3.jpg"]
                                                                  }];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifier = @"Cell";
    cell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[cell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    cell.imageView.image = [dic objectForKey:[_searchResults objectAtIndex:indexPath.row]];

    return cell;
}
3
  • Форматирование кода оставляет желать лучшего.
    – 0xdb
    2 ноя 2017 в 8:37
  • Поправил. Так лучше?
    – a.petrov
    2 ноя 2017 в 8:45
  • Да, так понятней.
    – 0xdb
    2 ноя 2017 в 8:49

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.