简体   繁体   中英

How to add image to tableviewcell

Sorry this is such a new question, but I searched all day and still cannot find the answer. When I run the app, it shows the rows with the label and count only, but the image is not showing up on the table view cell.

#import "ViewController.h"

@interface ViewController () <UITableViewDataSource,    UITableViewDelegate> {
NSMutableArray * imageNameArray;
//__weak IBOutlet UIImageView *cell;
__weak IBOutlet UITableView *Table;
}

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a     nib.
[self arraySetup];


}



-(void) arraySetup {
imageNameArray = [NSMutableArray arrayWithArray:@[@"2.jpg"]];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section {
return imageNameArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView  cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellId = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
//cell.imageView.image =  [UIImage imageNamed:imageNameArray [indexPath.row]];

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


cell.textLabel.text = imageNameArray [indexPath.row];
cell.detailTextLabel.text = [NSString stringWithFormat:@"%d", (int) indexPath.row + 1];

UIImageView *imv = [[UIImageView alloc]initWithFrame:CGRectMake(10,5, 50, 25)];
imv.image=[UIImage imageNamed:@"2.jpg"];
[cell.contentView addSubview:imv];
cell.imageView.image = imv.image;

return cell;

}

Here is some very basic code for you to learn from: I'm using the basic class UITableViewCell

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.textLabel.text = @"Your table row text";
    cell.imageView.image = [UIImage imageNamed:@"image.png"];// Here you specify your image
    cell.detailTextLabel.text = @"Your descriptive text";
    return cell;
} 

By default the UITableViewCell has the UIImageView . Try this:

- (UITableViewCell *)tableView:(UITableView *)tableView  cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellId = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];

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


cell.textLabel.text = imageNameArray [indexPath.row];
cell.detailTextLabel.text = [NSString stringWithFormat:@"%d", (int) indexPath.row + 1];

cell.imageView.image = [UIImage imageNamed:@"2.jpg"];
return cell;
}

Another approach is to use your own Custom TableViewCell.

add array in ViewDidLoad:

arrayValue = [[ "userEmailType": "Home", "userImage": "image1.png"], [ "userEmailType": "Home", "userImage": "image2.png"], [ "userEmailType": "Work", "userImage": "image3.png"],]

Custom UITableViewCell code:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell: CustomCell! = tableView.dequeueReusableCell(withIdentifier: "CustomCellIdentifier", for: indexPath) as! CustomCell
    // Static image
    cell.checkedImageObj!.image = UIImage.init(named: "resort_selected")   

    //Want round Image....  
    cell.contactImage.layer.cornerRadius = cell.contactImage.frame.size.width/2
    cell.contactImage.layer.masksToBounds = true

    // Set array of Image
    cell.contactImage.image = (arrayValue[indexPath.row] as! NSDictionary).value(forKey: "userImage") as? UIImage
}

Please try it.... Thanks

You must init the imageView at the init method of cell like:

if (cell == nil) {
    cell = [[UITableViewCell alloc]   initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
    UIImageView *imv = [[UIImageView alloc]initWithFrame:CGRectMake(10,5, 50, 25)];
    imv.tag = 10;
    [cell.contentView addSubview:imv];
}

and the you can set the image by

    cell.textLabel.text = imageNameArray [indexPath.row];
    cell.detailTextLabel.text = [NSString stringWithFormat:@"%d", (int) indexPath.row + 1];
    UIImageView *imv = (UIImageView *)[cell.contentView viewWithTag:10];
    imv.image=[UIImage imageNamed:@"2.jpg"];
    return cell;

I know this is very late reply and only few people are using Objective C these days .Below is working for me . Please note that I am reusing same tableView for populating address-books and when click on each address book loading it's contacts . So you can just remove if conditions.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"cells";
 UITableViewCell * cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
   
 
    
    if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        
                    }
    
     if (_isPhoneBooksLoaded){
         cell.imageView.image =nil;
     }
    
    if (_isContactsLoaded){
        
        cell.imageView.image = [UIImage imageNamed:@"contact_default.png"];
    }
   
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    
    
    return cell;
}

Important :

  1. Do not forget to use same cell identifier you set .

  2. Remove if conditions isPhoneBooksLoaded,isContactsLoaded and just keep

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

  1. Make sure you are using correct image file name and it exists in your resources .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM