0

我正在尝试将多个添加UIImageViewscrollView. imageview 的数量不同。这个数字是从文件中读取的。

所以现在我必须创建UIImageView*imageView1, UIImageView*imageView2, UIImageView*imageView3 ... UIImageView*imageViewN

如何将数字添加到*imageView(?)的末尾

任何帮助将不胜感激

4

3 回答 3

2

这不能按照您的想法进行。你应该做的是像这样使用 NSMutableArray:

NSMutableArray *imageViews = [[NSMutableArray alloc] init];
for(NSInteger i=0; i < 10; i++)
{
    UIImageView *currentImageView = [[UIImageView alloc] initWithFrame:CGRectMake((0 * (110 * i)), 0, 100, 100)];
    [currentImageView setImage:[UIImage imageNamed:@"test.png"]];
    [imageViews addObject:currentImageView];
}


//To reference the image views just call objectAtIndex
UIImageView *imageView1 = [imageViews objectAtIndex:0];
UIImageView *imageView2 = [imageViews objectAtIndex:1];

这也使您的代码更加灵活。

于 2012-08-06T06:32:41.363 回答
0

添加到上面的代码片段:

 NSString *imageName = [NSString stringWithFormat@"test_%d.png", i];
 [currentImageView setImage:[UIImage imageNamed:imageName]];

因此您可以添加具有不同图像的 UIImageViews

于 2012-08-06T06:36:21.270 回答
0

对于最近仍在搜索此主题的任何人,这是 Swift 3 版本,可以满足@Siddharthan 的要求:

    //you may give any Int value to these variables
    let Xposition = 19
    let Yposition = 19
    let imageWidth = 93
    let imageHeight = 128

    //create UIImageView object
    var curr_imageView = UIImageView()
    for i in 0 ..< images.count {
        if imageViews.isEmpty {
            curr_imageView = UIImageView.init(frame: CGRect(x: Xposition, y: Yposition, width: imageWidth, height: imageHeight))
        } else {
            curr_imageView = UIImageView.init(frame: CGRect(x: Int(imageViews[i-1].frame.origin.x)+Int(imageViews[i-1].frame.size.width), y: Yposition, width: imageWidth, height: imageHeight))
        }

        curr_imageView.image = UIImage(named: images[i])
        curr_imageView.contentMode = .scaleAspectFit
        self.scrollView1.addSubview(curr_imageView)
        imageViews.append(curr_imageView)
    }

    self.scrollView1.contentSize = CGSize(width: imageViews[imageViews.count-1].frame.size.width+imageViews[imageViews.count-1].frame.origin.x+CGFloat(Xposition), height: self.scrollView1.frame.size.height)
于 2017-06-09T22:51:33.103 回答