1

I am using below code to add number of images in a single cell content view.

    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"];  
.............................  
............................  
cell.imageView20.image = [UIImage imageNamed:@"testImg20"];  

How to use for loop with stringWithFormat to make my code look conscise something like:

for(imageCounter = 1; imageCounter <21, imageCounter++)  
{  
cell.imageView(imageCounter).image = [UIImage imageNamed:@"testImg(imageCounter)"];  
}  

Any Suggestion?

3
  • is all 20 imageview are in same cell??? Commented May 4, 2012 at 10:26
  • yes all 20 imageview(very small) are in same cell.I have subclassed UITableViewCell and made custom cell in my viewcontroller. Commented May 4, 2012 at 10:39
  • 1
    I put answer plz check and let me know if any clarification you need Commented May 4, 2012 at 10:40

4 Answers 4

2

To reach references of the different image views inside the loop you have to either out them in an array or tag them than you can either go:

  NSArray *cellImageViews;
  for(int imageCounter = 1; imageCounter < 21; imageCounter++)
  {
    [[cellImageViews objectAtIndex:imageCounter-1] setImage:[UIImage imageNamed:[NSString stringWithFormat:@"testImg%d", imageCounter]]];
  }

or

  for(int imageCounter = 1; imageCounter <21; imageCounter++)
  {
    [((UIImageView *)[cell viewWithTag:imageCounter]) setImage:[UIImage imageNamed:[NSString stringWithFormat:@"testImg%d", imageCounter]]];
  }
Sign up to request clarification or add additional context in comments.

1 Comment

hey both the way are working but second solution is just gawjus
1

You are using wrong logic.It cannot be use like this.Instead of this

for(imageCounter = 1; imageCounter <21, imageCounter++)
{
 cell.imageView(imageCounter).image = [UIImage imageNamed:@"testImg%d,(imageCounter)"];
}

You should use this :-

yourImageView = [[UIImageView alloc] initWithFrame:CGRectMake(x, y, width, height)];

for(imageCounter = 1; imageCounter <21, imageCounter++)
{
 [yourImageView setImage:[UIImage imageNamed:@"testImg%d",imageCounter]];
 cell.imageView = yourImageView;
}

Try this.Hope it gonna help you thanks :)

Comments

1

Hi you should first put all images in an array in cellForRowAtIndexPath method

NSMutableArray *imageArr=[[NSMutableArray alloc]initWithObject:cell.imageView1,cell.imageView2.....];

Then do the following:

for(imageCounter = 1; imageCounter <21, imageCounter++) {



[imageArr objectAtindex:imageCounter].image = [UIImage imageNamed:[NSString stringwithformat:@"testImg%d", imageCounter]];
}

let me know if any clarification you need

1 Comment

Appreciating your help.i am not able to access .image property here :[imageArr objectAtindex:imageCounter].image.However with set image as in above solution its working yee :)
1
UIImageView *img[20];//ur array of images

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

for (int i=0; i<20; i++) {

        //set frame  every time as ur requirement

        img[i].image = [UIImage imageNamed:[NSString stringWithFormat:@"testImg%d",i+1]];
        [cell.contentView addSubview:img[i]];
    }
}

I think this code work perfectly For More Examples for better Description.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.