Подтвердить что ты не робот

Программно добавить изображение в ячейку TableView

У меня есть UITableView, который получает информацию с сервера и публикует данные в виде таблицы. Внутри каждой ячейки находится информация с сервера.

Для целей этого примера, скажем, информация, которую мы получаем с сервера, это числа: 1, 2, 3, 4.

То, что я хочу сделать, - это добавить изображение (программно, потому что есть какие-либо операторы и т.д.) в левой части ячейки, а также текст (1, 2 и т.д.) рядом с ним.

В принципе, я хочу, чтобы каждая ячейка выглядела так:

 _____________________________________
| (IMAGE) (TEXT)                      | --CELL 0
--------------------------------------
 _____________________________________
| (IMAGE) 2                           | --CELL 1
--------------------------------------

Пожалуйста, извините мою грубую иллюстрацию.:)

4b9b3361

Ответ 1

здесь вы идете:

cell.imageView.image = [UIImage imageNamed:@"image.png"];

Swift:

cell.imageView?.image = UIImage(named: "image.png")

Ответ 2

Вы можете добавить как изображение, так и текст в UITableViewCell в подклассу UITableViewController, например:

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    [[cell imageView] setImage:anImage];
    [[cell textLabel] setText:[NSString stringWithFormat:@"%d",[indexPath row]];

     return cell;
}

В UITableViewCell есть несколько встроенных свойств, которые вы можете использовать, например, imageView, textLabel и detailTextLabel. Для получения дополнительной информации см. Руководство по программированию таблиц.

Ответ 3

Версия первого ответа Swift 2.0:

cell.imageView?.image = UIImage(named: "image.png")

Ответ 4

Объекты UITableViewCell имеют свойство imageView; вы можете просто установить это изображение изображения, и оно автоматически отобразит изображение в нужном месте с названием рядом с ним.

Ответ 5

// create a rectangle box for image view

UIImageView *iconImage = [[UIImageView alloc] init];
iconImage.frame = CGRectMake(12,4,53.5,53.5);

// Create a label for cells
UILabel *cellName = [[UILabel alloc] initWithFrame:CGRectMake(75,18,200,20)];
cellName.font = [self fontForBodyTextStyle];

NSString *cellNameStr = [NSString stringWithFormat:@"%@",self.tableCellNames[indexPath.row]];

// Select path row tab actions
switch (indexPath.row) {
    case 0:

        cellName.text = cellNameStr;
        iconImage.image = [UIImage imageNamed:@"condition.png"];
        [cell.contentView addSubview:iconImage];
        [cell.contentView addSubview:cellName];

        break;

    case 1:
       cellName.text = cellNameStr;
        iconImage.image = [UIImage imageNamed:@"videos.png"];
        [cell.contentView addSubview:iconImage];
        [cell.contentView addSubview:cellName];
        break;

    case 2:
        cellName.text = cellNameStr;
        iconImage.image = [UIImage imageNamed:@"provider.png"];
        [cell.contentView addSubview:iconImage];
        [cell.contentView addSubview:cellName];
        break;

    case 3:
        cellName.text = cellNameStr;
        iconImage.image = [UIImage imageNamed:@"info.png"];
        [cell.contentView addSubview:iconImage];
        [cell.contentView addSubview:cellName];
        break;

    default:
        NSAssert(NO, @"Unhandled value in cellForRowAtIndexPath");
        break;
}