23

我有一个UITableView从服务器获取信息并将数据发布到表格视图中。每个单元格内部都是来自服务器的信息。

就本示例而言,假设我们从服务器获取的信息是数字:1、2、3、4。

我想要做的是将图像(以编程方式,因为涉及 if 语句等)添加到单元格的左侧,并在其旁边添加文本(1、2 等)。

基本上,我希望每个单元格看起来像这样:

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

请原谅我的粗略说明。:)

4

5 回答 5

21

here you go:

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

Swift:

cell.imageView?.image = UIImage(named: "image.png")
于 2011-08-24T17:24:12.257 回答
8

You can add both the image and the text to the UITableViewCell in your UITableViewController subclass like so:

- (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;
}

There are a few "built-in" properties to UITableViewCell that you can take advantage of, such as the imageView, textLabel, and detailTextLabel. For more information, check out the Table View Programming Guide.

于 2011-08-24T17:23:17.350 回答
5

第一个答案的 Swift 2.0 版本是:

cell.imageView?.image = UIImage(named: "image.png")
于 2015-09-24T08:54:27.133 回答
4

UITableViewCell objects have an imageView property; you can just set this image view's image and it will automatically display the image in the right place with the title next to it.

于 2011-08-24T17:22:44.360 回答
0
// 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;
}
于 2013-10-25T09:21:51.997 回答