How to add images to UITableView cell?

后端 未结 5 1369
暖寄归人
暖寄归人 2021-02-09 16:57

I temporarly added some values to the UITableViewCell programmatically. But I need to add images to each cell. How can I do that?

Here is my code. .h file

相关标签:
5条回答
  • 2021-02-09 17:11

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

    Thank VKJ,

    0 讨论(0)
  • 2021-02-09 17:14

    you adding it right, but you fogot one thing:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
        static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: SimpleTableIdentifier];
        if (cell == nil) { cell = [[[UITableViewCell alloc]
                                    initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SimpleTableIdentifier] autorelease];
        }
        NSUInteger row = [indexPath row]; cell.textLabel.text = [listData objectAtIndex:row];
    
        UIImageView *imv = [[UIImageView alloc]initWithFrame:CGRectMake(3,2, 20, 25)];
        imv.image=[UIImage imageNamed:@"user.jpg"];
        [cell.contentView addSubview:imv];
        [imv release];
    
        //you fogot this:
        return cell;
         }
    
    0 讨论(0)
  • 2021-02-09 17:21

    Move return cell; at the end of the method:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
        static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: SimpleTableIdentifier];
        if (cell == nil) { cell = [[[UITableViewCell alloc]
                                    initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SimpleTableIdentifier] autorelease];
        }
        NSUInteger row = [indexPath row]; 
        cell.textLabel.text = [listData objectAtIndex:row]; 
    
    
        UIImageView *imv = [[UIImageView alloc]initWithFrame:CGRectMake(3,2, 20, 25)];
        imv.image=[UIImage imageNamed:@"user.jpg"];
        [cell.contentView addSubview:imv];
        [imv release];
    
        return cell;
    }
    
    0 讨论(0)
  • 2021-02-09 17:21

    You can add images using the imageView property of a cell:

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

    in your - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath method.

    0 讨论(0)
  • 2021-02-09 17:29

    Here's an example cell loading a facebook image in a table cell.

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil)
        {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
                                           reuseIdentifier:CellIdentifier] autorelease];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
    
        // Leave cells empty if there's no data yet
        if (nodeCount > 0)
        {
            // Set up the cell...
            AppRecord *appRecord = [self.friendsList objectAtIndex:indexPath.row];
    
            cell.textLabel.text = friend.name;
    
            // Only load cached images; defer new downloads until scrolling ends
            if (!appRecord.icon)
            {
                // set the url
                appRecord.imageURLString = [NSString stringWithFormat:@"http://graph.facebook.com/%@/picture", friend.recordID];
    
                // request the download
                if (self.tableView.dragging == NO && self.tableView.decelerating == NO)
                {
                    [self startIconDownload:appRecord forIndexPath:indexPath];
                }
                // if a download is deferred or in progress, return a placeholder image
                cell.imageView.image = [UIImage imageNamed:@"Placeholder.png"];                
            }
            else
            {
                cell.imageView.image = appRecord.icon;
            }
    
        }
    
        return cell;
    
    0 讨论(0)
提交回复
热议问题