How to set a tag on UIImageview Image is equal to Array Index for every image in iOS?

advertisements

i am newbie in iOS Development. i want to set tag an imageview image is equal to its value in my Array index like as my Array Contain 50 image link and i set a imageview to image code like as

for(int index=0; index < [self.imagesa count]; index++)
{
    NSDictionary *dict=[self.imagesa objectAtIndex:index];
    NSString *image=[dict valueForKey:@"link"];
    smallImage = [[UIImageView alloc] init];
    pageLabel=[[UILabel alloc]init];
    smallImage.tag=index;
    smallImage.bounds = CGRectMake(10, 10, self.scrollView.frame.size.width, self.scrollView.frame.size.width);
    smallImage.frame = CGRectMake(5+xOffset, 0, 50, 50);
    [smallImage sd_setImageWithURL:[NSURL URLWithString:image] placeholderImage:[UIImage imageNamed:@"1.png"]];
    [self.imageArray insertObject:smallImage atIndex:index];
    CGSize scrollViewSize=CGSizeMake(self.scrollView.frame.size.width*[self.imageArray count], self.scrollView.frame.size.height);
    [self.scrollView setContentSize:scrollViewSize];
    CALayer *borderLayer = [CALayer layer];
    CGRect borderFrame = CGRectMake(10, 10, (smallImage.frame.size.width), (smallImage.frame.size.height));
    [borderLayer setBackgroundColor:[[UIColor clearColor] CGColor]];
    [borderLayer setFrame:borderFrame];
    [borderLayer setCornerRadius:kCornerRadius];
    [borderLayer setBorderWidth:kBoarderWidth];
    [borderLayer setBorderColor:[[UIColor redColor] CGColor]];
    [smallImage.layer addSublayer:borderLayer];
    [self.scrollView addSubview:smallImage];
    [self.scrollView addSubview:pageLabel];
    [self.scrollView addSubview:[self.imageArray objectAtIndex:index]];
    xOffset += 60;
}

Here i want like as if i select 5th image then it tag is also Five how it possible?here i set imageview.tag=index but it is always Returen as my Array Count Value. PLease give me Solution FOr Each imagetag is Equal to its Index in Array.

thnx in advance.


I have improved your code and added a UIButton as a click area:

self.imageArray = [[NSMutableArray alloc] init];

for(int index=0; index < [self.imagesa count]; index++){
    NSDictionary *dict=[self.imagesa objectAtIndex:index];
    NSString *imageString = [dict valueForKey:@"link"];

    UIImageView *smallImage = [[UIImageView alloc] init];
    smallImage.bounds = CGRectMake(10, 10, self.scrollView.frame.size.width, self.scrollView.frame.size.width);
    smallImage.frame = CGRectMake(5+(index*60), 0, 50, 50);
    [smallImage sd_setImageWithURL:[NSURL URLWithString:imageString] placeholderImage:[UIImage imageNamed:@"1.png"]];
    smallImage.layer.backgroundColor = [[UIColor clearColor] CGColor];
    smallImage.layer.cornerRadius = kCornerRadius;
    smallImage.layer.borderWidth = kBoarderWidth;
    smallImage.layer.borderColor = [[UIColor redColor] CGColor];
    [self.imageArray addObject:smallImage];
    [self.scrollView addSubview:smallImage];

    UIButton *clickArea = [UIButton buttonWithType:UIButtonTypeCustom];
    clickArea.frame = smallImage.frame;
    clickArea.tag = index;
    [clickArea addTarget:self action:@selector(clickAreaClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.scrollView addSubview:clickArea];
}

// Doesn't need to be in for-loop
CGSize scrollViewSize=CGSizeMake(self.scrollView.frame.size.width*[self.imageArray count], self.scrollView.frame.size.height);
[self.scrollView setContentSize:scrollViewSize];

And to get the tag from the UIButton click:

- (void)clickAreaClicked:(UIButton*)sender
{
    NSLog(@"sender tag: %i", sender.tag );
}

I have just tested it and it works. Let me know if it works for you.