从两个图像阵列填充两个集合视图



所以我一直在四处搜索,尽管有很多线程解释了我所需要的内容,但我仍然无法成功执行我的代码。我在ViewController上有两个集合视图,一个有6个单元格,另一个有32个单元格(从上到下4个,8个单元格)。每个集合视图都需要从3个图像的数组中填充,它们是相同的图像,只是不同的颜色,我想让它们随机填充。我有两个cell.xib文件,每个集合视图一个。下面是我正在使用的ViewController的.m文件。

#import "FirstViewController.h"
#import "CollectionViewCell.h"
#import "RemotesCollectionCell.h"
#import <stdlib.h>
@interface FirstViewController ()

@end
@implementation FirstViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    UINib *cellNib = [UINib nibWithNibName:@"CollectionViewCell" bundle:nil];
    [self.headerViewCollection registerNib:cellNib forCellWithReuseIdentifier:@"collectCell"];
    UINib *cellNibRemote = [UINib nibWithNibName:@"RemotesCollectionCell" bundle:nil];
    [self.remoteViewCollection registerNib:cellNibRemote forCellWithReuseIdentifier:@"collectCell"];
    self.headLabel.text=[NSString stringWithFormat:@"HnEnAnD"];
    self.remotesLabel.text = [NSString stringWithFormat:@"RnEnMnOnTnEnS"];
    self.title = [NSString stringWithFormat:@"OTIS"];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
};
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if(collectionView == self.headerViewCollection){
        return 6;
    }
    else return 32;
};
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collectCell" forIndexPath:indexPath];

    return cell;
};
@end

这是一个cell.xib文件的.h文件

#import <UIKit/UIKit.h>
@interface RemotesCollectionCell : UICollectionViewCell
@property (strong, nonatomic) IBOutlet UIImageView *remoteImageView;

@end

这是笔尖的.m文件。

#import "RemotesCollectionCell.h"
#import "FirstViewController.h"
@implementation RemotesCollectionCell
- (void)awakeFromNib {
    // Initialization code
    NSMutableArray *remoteImages = [[NSMutableArray alloc]initWithObjects:[UIImage imageNamed:@"red-gps.png"], [UIImage imageNamed:@"green-gps.png"], [UIImage imageNamed:@"orange-gps.png"], nil];
    _remoteImageView = [[UIImageView alloc]initWithFrame:self.contentView.bounds];
    [self.contentView addSubview:_remoteImageView];
    NSLog(@"%lu", (unsigned long)remoteImages.count);
}

@end

我为所有的代码道歉,但我希望它能帮助你帮助我,提前谢谢你。请原谅我的失误。

//In wakeFromNib方法定义为_remoteImageView.tag=1;以便您可以在cellForIndexPath 中访问

    - (void)awakeFromNib {
        // Initialization code
        NSMutableArray *remoteImages = [[NSMutableArray alloc]initWithObjects:[UIImage imageNamed:@"red-gps.png"], [UIImage imageNamed:@"green-gps.png"], [UIImage imageNamed:@"orange-gps.png"], nil];
        _remoteImageView = [[UIImageView alloc]initWithFrame:self.contentView.bounds];
_remoteImageView.tag=1;
        [self.contentView addSubview:_remoteImageView];
        NSLog(@"%lu", (unsigned long)remoteImages.count);
    }

//这里,每当单元加载时,在随机化阵列之后加载图像。

      -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
                UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collectCell" forIndexPath:indexPath];
NSMutableArray *remoteImages = [[NSMutableArray alloc]initWithObjects:@"red-gps.png", @"green-gps.png", @"orange-gps.png", nil];

 int count = (int)[remoteImages count];
    for (int i = 0; i < count; ++i) {
        // Select a random element between i and end of array to swap with.
        int nElements = count - i;
        int n = (arc4random() % nElements) + i;
        [remoteImages exchangeObjectAtIndex:i withObjectAtIndex:n];
    }
NSLog(@"remoteImages=%@",remoteImages);
                UIImageView *imageView = (UIImageView*)[cell.contentView viewWithTag:1];
                imageView.image=[UIImage imageNamed:[remoteImages objectAtIndex:0]];

                return cell;
            };

最新更新