如何将标签设置为UIImageview图像等于iOS中每个图像的数组索引?
我是iOS开发的新手。我想设置标记imageview图像等于其在数组索引中的值,就像我的数组包含50个图像链接一样,我将imageview设置为图像代码,就像如何将标签设置为UIImageview图像等于iOS中每个图像的数组索引?,ios,objective-c,arrays,uiimageview,Ios,Objective C,Arrays,Uiimageview,我是iOS开发的新手。我想设置标记imageview图像等于其在数组索引中的值,就像我的数组包含50个图像链接一样,我将imageview设置为图像代码,就像 for(int index=0; index < [self.imagesa count]; index++) { NSDictionary *dict=[self.imagesa objectAtIndex:index]; NSString *image=[dict valueForKey:@"link"];
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;
}
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];
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]占位符图像:[UIImage ImageName:@“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*边界层=[CALayer层];
CGRect borderFrame=CGRectMake(10,10,(smallImage.frame.size.width),(smallImage.frame.size.height));
[borderLayer setBackgroundColor:[[UIColor clearColor]CGColor]];
[边界层设置框架:边界框架];
[边界层设置角半径:kCornerRadius];
[borderLayer SetBoardWidth: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;
}
在这里,我想好像我选择了第五个图像,那么它的标签也是五个,怎么可能呢?在这里,我设置了imageview.tag=index,但它总是作为我的数组计数值返回。请给我每个imagetag等于其数组索引的解决方案
thnx。您需要在scrollview中添加按钮并设置标签
for(int index=0; index < [self.imagesa count]; index++)
{
NSDictionary *dict=[self.imagesa objectAtIndex:index];
NSString *image=[dict valueForKey:@"link"];
smallImage = [[UIImageView alloc] init];
imageButton =[UIButton buttonWithType:UIButtonTypeCustom];
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);
imageButton.frame=CGRectMake(5+xOffset, 0, 50, 50);
imageButton.tag=index;
[imageButton addTarget:self action:@selector(imageClicked:) forControlEvents:UIControlEventTouchUpInside];
[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:imageButton];
[self.scrollView addSubview:pageLabel];
[self.scrollView addSubview:[self.imageArray objectAtIndex:index]];
xOffset += 60;
}
for(int index=0;index<[self.imagesa count];index++)
{
NSDictionary*dict=[self.imagesa objectAtIndex:index];
NSString*image=[dict valueForKey:@“link”];
smallImage=[[UIImageView alloc]init];
imageButton=[UIButton按钮类型:UIButtonTypeCustom];
pageLabel=[[UILabel alloc]init];
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);
frame=CGRectMake(5+xOffset,0,50,50);
tag=索引;
[imageButton addTarget:自我操作:@选择器(imageClicked:)forControlEvents:UIControlEventTouchUpInside];
[smallImage sd_setImageWithURL:[NSURL URLWithString:image]占位符图像:[UIImage ImageName:@“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*边界层=[CALayer层];
CGRect borderFrame=CGRectMake(10,10,(smallImage.frame.size.width),(smallImage.frame.size.height));
[borderLayer setBackgroundColor:[[UIColor clearColor]CGColor]];
[边界层设置框架:边界框架];
[边界层设置角半径:kCornerRadius];
[borderLayer SetBoardWidth:kBoarderWidth];
[borderLayer setBorderColor:[[UIColor redColor]CGColor]];
[smallImage.layer addSublayer:borderLayer];
[self.scrollView addSubview:smallImage];
[self.scrollView addSubview:imageButton];
[self.scrollView addSubview:pageLabel];
[self.scrollView addSubview:[self.imageArray objectAtIndex:index]];
xOffset+=60;
}
//(或)
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];
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]占位符图像:[UIImage ImageName:@“1.png”];
UITapGestureRecognizer*tapRecognizer=
[[UITapGestureRecognitizer alloc]initWithTarget:self
操作:@selector(handleSingleTap:)];
[TapRecognitor setNumberOfTouchesRequired:1];
[smallImage AddGestureRecognitor:TapRecognitor];
[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*边界层=[CALayer层];
CGRect borderFrame=CGRectMake(10,10,(smallImage.frame.size.width),(smallImage.frame.size.height));
[borderLayer setBackgroundColor:[[UIColor clearColor]CGColor]];
[边界层设置框架:边界框架];
[边界层设置角半径:kCornerRadius];
[borderLayer SetBoardWidth: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;
}
-(无效)handleSingleTap:(UITapGestureRecognitor*)识别器
{
UIImageView*当前图像=(UIImageView*)[识别器视图];
NSLog(@“%d”,currentimage.tag);
}
您可以在imageClicked按钮操作处获取标记
希望它能帮助您……您需要在scrollview中添加按钮并设置标签
for(int index=0; index < [self.imagesa count]; index++)
{
NSDictionary *dict=[self.imagesa objectAtIndex:index];
NSString *image=[dict valueForKey:@"link"];
smallImage = [[UIImageView alloc] init];
imageButton =[UIButton buttonWithType:UIButtonTypeCustom];
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);
imageButton.frame=CGRectMake(5+xOffset, 0, 50, 50);
imageButton.tag=index;
[imageButton addTarget:self action:@selector(imageClicked:) forControlEvents:UIControlEventTouchUpInside];
[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:imageButton];
[self.scrollView addSubview:pageLabel];
[self.scrollView addSubview:[self.imageArray objectAtIndex:index]];
xOffset += 60;
}
for(int index=0;index<[self.imagesa count];index++)
{
NSDi
smallImage = (UIImageView*)self.imagesa[index]
UIImageView* smallImage = [[UIImageView alloc] init];
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];
- (void)clickAreaClicked:(UIButton*)sender
{
NSLog(@"sender tag: %i", sender.tag );
}