【问题标题】:How to set tag to UIImageview Image is equal to Array Index for each Image in iOS?如何将标签设置为 UIImageview 图像等于 iOS 中每个图像的数组索引?
【发布时间】:2014-11-24 10:35:23
【问题描述】:

我是 iOS 开发的新手。我想设置标签 imageview 图像等于它在我的 Array 索引中的值,就像我的 Array Contain 50 image 链接一样,我将 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 个图像然后它的标签也是 5 怎么可能?在这里我设置 imageview.tag=index 但它总是 Returen 作为我的数组计数值。请给我解决方案每个图像标签都等于它在数组中的索引。

请提前。

【问题讨论】:

  • 你初始化self.imageArray了吗?
  • 是的@Gismay 我初始化了它。它总是设置我数组索引的最后一个索引。

标签: ios objective-c arrays uiimageview


【解决方案1】:

我改进了您的代码并添加了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 );
}

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

【讨论】:

    【解决方案2】:

    我假设您的 smallImage 变量是在该循环之外声明的?如果是这样,您只是每次都覆盖它并将 smallImage.tag=index 设置为当前索引,因此每次它被覆盖并插入到您的数组中时,它将始终是循环中的最后一个索引。

    如果你想在你的 self.imagesa 数组中访问 UIImageView,你需要这样做

    smallImage = (UIImageView*)self.imagesa[index] 
    

    这将访问索引处的 UIImageView。 否则,如果您想每次都创建一个新的 UIImageView ,则需要这样做

    UIImageView* smallImage = [[UIImageView alloc] init];
    

    替换循环内的现有分配。

    希望这会有所帮助。

    【讨论】:

    • 当我替换我的 imageview 声明 smallImage = (UIImageView*)self.imagesa[index] 和 Set smallImage.tag=index;然后它在 smallimage.tag=index 中显示错误。请给我解决方案。
    • smallImage 真的是 UIImageView 的一个实例吗?如果不是,并且如果它不是 UIView 的任何其他子类,它就会崩溃。您实际上在该数组中存储了什么?它是 UIImage 还是 UIImageView?如果是 UIImage 那么你需要使用我建议的后一种方法: UIImageView* smallImage = [[UIImageView alloc] init];
    【解决方案3】:

    您需要添加按钮到滚动视图并为其设置标签..

    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];
        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"]];
        UITapGestureRecognizer *tapRecognizer =
        [[UITapGestureRecognizer alloc] initWithTarget:self
                                                    action:@selector(handleSingleTap:)];
        [tapRecognizer setNumberOfTouchesRequired:1];
    
            [smallImage addGestureRecognizer:tapRecognizer];
        [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;
    }
    
    - (void)handleSingleTap:(UITapGestureRecognizer *)recognizer
    {
        UIImageView *currentimage = (UIImageView *)[recognizer view];
        NSLog(@"%d",currentimage.tag);
    }
    

    您可以在 imageClicked 按钮操作处获取标签..

    希望对你有帮助...

    【讨论】:

    • 我尝试过,但当我在触摸事件上编写代码时对我不起作用,例如 -(IBAction)imageClicked:(id)sender { NSLog(@"Button Tag %d",imageButton.tag ); } 那么它就是 Print nothing.
    • 你需要检查sender.tag或者[sender tag]; NSLog(@"按钮标签 %d",[发送者标签]);
    • 我也尝试过,但它没有打印任何东西。
    • 我修改了代码检查它..上面的日志肯定会工作..!
    • 我想在 MyIamgeView 的子视图中添加一个按钮,就像我在代码中添加子层一样,那么它可以工作吗?
    猜你喜欢
    • 1970-01-01
    • 2017-04-13
    • 2020-04-13
    • 2011-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-15
    相关资源
    最近更新 更多