简体   繁体   中英

How to add images in UITableViewCells programmatically in iOS

I need to add different images in UITableViewCells programmatically. How can I do that. I am trying some code. But the images are not displayed in UITableViewCells . This is my code below.

-(void)viewDidLoad
{
    arrImages=[[NSMutableArray alloc]initWithObjects:@"image.png",@"image2.png",@"image3.png",@"image4.png",@"image5.png",@"image6.png",@"image7.png",@"image8.png" nil];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *identifier=@"cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];

    if (cell == nil) 

    {

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"identifier"];

    }

    imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 48, 48)];

    cell.imageView.image = [UIImage imageNamed:[arrImages objectAtIndex:indexPath.row]];

    return cell;
}

This is what you probably want:

- (void)viewDidLoad
{
    self.arrImages = [[NSMutableArray alloc] initWithObjects:@"image.png",@"image2.png",@"image3.png",@"image4.png",@"image5.png",@"image6.png",@"image7.png",@"image8.png" nil];
}

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

    cell.imageView.image = [UIImage imageNamed:[self.arrImages objectAtIndex:indexPath.row]];

    return cell;
}

PS : Next time, please make some effort on formatting the source code, explaining what is wrong, what have you tried, what would you like to accomplish. Thanks

Create a property of NSArray in your .h file

@property NSArray *imgArray;

synthesize in your .m file

@synthesize imgArray;

write this in your viewDidLoad

- (void)viewDidLoad
{
    [super viewDidLoad];


    // Initialize images...
    imgArray = [NSArray arrayWithObjects:@"yourFirstImageName.png", @"yourSecondImageName.png", @"yourThirdImageName.png",....., @"yourSeventhImageName.png", nil];
}

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

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


        UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 48, 48)]; // your cell's height should be greater than 48 for this.
        imgView.tag = 1;
        [cell.contentView addSubview:imgView];
        imgView = nil;

        UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(x, y, width, height)];

        lbl.backgroundColor = [UIColor clearColor];
        [lbl setTag:2];
        [cell.contentView addSubview:lbl];
        lblDisch = nil;
    }

    UIImageView *_imgView = (UIImageView *)[cell.contentView viewWithTag:1];
    _imgView.image = [UIImage imageNamed:[imgArray objectAtIndex:indexPath.row]];

    UILabel *_lbl = (UILabel *)[cell.contentView viewWithTag:2];
    _lbl.text =  [arrFont objectAtIndex:indexPath.row]; // arrFont is your array
    return cell;
}

Hope this will help you.

Create a @property of NSMutableArray in your .h file.

NSMutableArray *data;

Write This Code your .m file.

- (void)viewDidLoad
{
    data = [[NSMutableArray alloc]initWithObjects:@"1.jpg",@"2.jpg",@"3.jpg",@"4.jpg",@"5.jpg",@"6.jpg",@"7.jpg",@"8.jpg",@"9.jpg",@"10.jpg",@"11.jpg",@"12.jpg",@"13.jpg",nil];
    [super viewDidLoad];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [data count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"cellID";

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

    cell.imageView.image = [UIImage imageNamed:[data objectAtIndex:indexPath.row]];

    return cell;
}

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