How to properly add an image programmatically in a customcell objective-c

63 Views Asked by At

I'm trying to add an image programmatically in a custom tableviewcell but it display it like this:

1

I would like it like this:

2 (like a notification)

Here is my code:

ViewController.m

@implementation albumsSingerVC

- (void)viewDidLoad{
    _matableview.delegate = self;
    _matableview.dataSource = self;
}

- ( nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    static NSString *MyIdentifier = @"customCell";
        
      customCellSingersAlbumVC *cell = (customCellSingersAlbumVC*)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];
        
      if (cell == nil)
      {
          [tableView registerNib:[UINib nibWithNibName:@"customCellSingersAlbumView" bundle:nil] forCellReuseIdentifier:MyIdentifier];
    
                 
      }
cell = (customCellSingersAlbumVC *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier]; 

    UIImageView *imageview = [[UIImageView alloc] init];
    UIImage *myimg = [UIImage imageNamed:@"pink.png"];
    imageview.image=myimg;
    imageview.frame = CGRectMake(50, 50, 150, 150); //I also 
changed the values to a small ones.
    [[cell imageView ] addSubview:imageview]; 
   
    return cell;
    
}
    
@end


I tried it with differents syntax.

I also tried to do it different :

customCell.m:

@implementation customCellSingersAlbumVC

- (void)viewDidLoad{
    
    UIImageView *imageview = [[UIImageView alloc] init];
    UIImage *myimg = [UIImage imageNamed:@"pink.png"];
    imageview.image=myimg;
    imageview.frame = CGRectMake(50, 50, 150, 150);
    [self.contentView addSubview:imageview];
    
}

@end

but viewdidload is never triggered.

1

There are 1 best solutions below

2
Six On

Hi I finally found how to do it here is the code if someone is interested:


    - ( nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
        static NSString *MyIdentifier = @"customCell";
            
          customCellSingersAlbumVC *cell = (customCellSingersAlbumVC*)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];
            
          if (cell == nil)
          {
              [tableView registerNib:[UINib nibWithNibName:@"customCellSingersAlbumView" bundle:nil] forCellReuseIdentifier:MyIdentifier];
        
                     
          }
        cell = (customCellSingersAlbumVC *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];
        
        // Create a new UIImage object
        UIImage *image = [UIImage imageNamed:@"pink.png"];
    
        // Create a new image view 
        UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    
        // Set the image view's frame to the desired size
        imageView.frame = CGRectMake(95, 10, 30, 30);
    
        // Create a new label object
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 20)];
    
        // Set the label's text
        label.text = @"  6";
      //  imageView.tintColor = UIColor.blueColor;
        imageView.layer.cornerRadius = imageView.frame.size.width /2;
        imageView.layer.masksToBounds = YES;
        imageView.layer.borderWidth = 0;
    
        // Add the image view to your table view cell's content view
        [imageView addSubview:label];
        [cell.contentView addSubview:imageView];
      
       
        return cell;
        
    }