9

Adding Multiple Images Dynamically to TableView

 3 years ago
source link: https://www.codesd.com/item/adding-multiple-images-dynamically-to-tableview.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Adding Multiple Images Dynamically to TableView

advertisements

I have fairly started with IOS development and am on my first client project.

I am stuck at the following place:

so if there are 10 people's birthday this month, i need to display 10 imageviews in a single row (3 pictures, 3 pictures and 1 picture).

Can someone help me with the above. It will be greatly appreciated.


If you want to add images/labels/buttons in tableview, then the simplest way is to subclass UITableViewCell.For Example if you have 5 images(didn't understand 3 pictures, 3 pictures and 1 picture ) and you want to show them in a table row then you could do something like this:

  1. Add a new file in your project named CustomCell which should be the subclass of UITableviewCell like this:

    @interface CustomCell : UITableViewCell {  
    
        UIImageView *imageView1;
        UIImageView *imageView2;
        UIImageView *imageView3;
        UIImageView *imageView4;
        UIImageView *imageView5;
    }
    @property (nonatomic, retain) UIImageView *imageView1;
    @property (nonatomic, retain) UIImageView *imageView2;
    @property (nonatomic, retain) UIImageView *imageView3;
    @property (nonatomic, retain) UIImageView *imageView4;
    @property (nonatomic, retain) UIImageView *imageView5;
    @end
    
    
  2. And in CustomCell.m put this code like this:

    @synthesize imageView1, imageView2, imageView3, imageView4, imageView5;
    - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
    {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
        {
            self.imageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(10, 8, 50, 45)];
            [self.contentView addSubview:imageView1];
    
            self.imageView2 = [[UIImageView alloc] initWithFrame:CGRectMake(65, 8, 50, 45)];
            [self.contentView addSubview:imageView2];
    
            self.imageView3 = [[UIImageView alloc] initWithFrame:CGRectMake(120, 8, 50, 45)];
            [self.contentView addSubview:imageView3];
    
            self.imageView4 = [[UIImageView alloc] initWithFrame:CGRectMake(170, 8, 50, 45)];
            [self.contentView addSubview:imageView4];
    
            self.imageView5 = [[UIImageView alloc] initWithFrame:CGRectMake(225, 8, 50, 45)];
            [self.contentView addSubview:imageView5];
        }
    
    return self;
    }
    
    

So you are done with subclassing of UITableViewCell.Now how to use them in your view controller?
You have to import CustomCell.h in your View Controller class.And in the cellForRowAtIndexPath delegate method do some thing like this:

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

        //our custom cell
        CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }  

        // Configure the cell.  

        //cell.imageView1.image = [UIImage imageNamed:@"testImg1"];
        //cell.imageView2.image = [UIImage imageNamed:@"testImg2"];
        //cell.imageView3.image = [UIImage imageNamed:@"testImg3"];
        //cell.imageView4.image = [UIImage imageNamed:@"testImg4"];
        //cell.imageView5.image = [UIImage imageNamed:@"testImg5"];
        //return cell;

u could use the better was :

    NSMutableArray *imgArray = [[NSMutableArray alloc] initWithObjects:cell.imageView1, cell.imageView2,cell.imageView3, cell.imageView4, cell.imageView5, nil];

    for (int imageCounter = 0; imageCounter<5; imageCounter++) {
        [[imgArray objectAtIndex:imageCounter] setImage:[UIImage imageNamed:[NSString stringWithFormat:@"testImg%d", imageCounter]]];
            }
return cell;
}

this is the very prototype solution for your problem's context.
Hope it would help you :)


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK