Как загрузить изображения в выбранные строки в UITableView? - PullRequest
0 голосов
/ 19 ноября 2011

Я пытаюсь загрузить изображение в строку таблицы. Сценарий таков: у меня есть два массива, скажем

nameID = [NSString stringWithFormat:@"01",@"02",@"05",@"06",@"07",@"08",@"09",nil];

и

testID = [NSString stringWithFormat:@"02",@"05",@"07",@"09",nil];

и я загрузил значения nameID в таблицу. Теперь мне нужно загрузить изображения в строку, если значение indexPath.row содержится в testID. Я попытался загрузить, сначала он работает, но при прокрутке изображение загружается во все строки при прокрутке. Итак, как я могу реализовать это надлежащим образом? Вот мой код:

NSString *temp1 = [friendsID objectAtIndex:indexPath.row];
if ([alertArray containsObject:temp1]) {
    tickImg.image = [UIImage imageNamed:@"tick.png"];
    [cell.contentView addSubview:tickImg];
}

Пожалуйста, поделитесь своими мыслями. Спасибо.

1 Ответ

1 голос
/ 19 ноября 2011

Надеюсь, этот фрагмент поможет вам.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellIdentifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];
    }
    //configure cell
    NSString *imageName = [NSString stringWithFormat:@"%02d",indexPath.row]; //convert row number to string with format ##
    if ([testId containsObject:imageName]) {
        UIImage *img = [UIImage imageNamed:imageName];
        cell.imageView.image = img;
    }
    cell.textLabel.text = [nameId objectAtIndex:indexPath.row];

    return cell;
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...