gpt4 book ai didi

ios - 如何将标签设置为 UIImageview 图像等于 iOS 中每个图像的数组索引?

转载 作者:行者123 更新时间:2023-11-28 19:54:50 25 4
gpt4 key购买 nike

我是 iOS 开发新手。我想设置一个 imageview 图像的标签等于它在我的 Array 索引中的值,就像我的 Array Contain 50 图像链接一样,我将 imageview 设置为图像代码,如

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;
}

在这里我想好像我选择了第 5 个图像然后它的标签也是五个怎么可能?在这里我设置了 imageview.tag=index 但它总是 Returen 作为我的数组计数值。请给我解决方案,因为每个图像标签都等于它在数组中的索引。

提前谢谢

最佳答案

我已经改进了你的代码并添加了一个 UIButton 作为点击区域:

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];

要从 UIButton 中获取标签,请单击:

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

我刚刚测试过它并且有效。让我知道它是否适合您。

关于ios - 如何将标签设置为 UIImageview 图像等于 iOS 中每个图像的数组索引?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27102738/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com