所以我一直在寻找,虽然有许多线程解释接近我需要的东西,但我仍然无法成功执行我的代码。我在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:@"H\nE\nA\nD"];
self.remotesLabel.text = [NSString stringWithFormat:@"R\nE\nM\nO\nT\nE\nS"];
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
这是我的.h文件中的一个cell.xib文件
#import <UIKit/UIKit.h>
@interface RemotesCollectionCell : UICollectionViewCell
@property (strong, nonatomic) IBOutlet UIImageView *remoteImageView;
@end
这是nib的.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
我为所有代码道歉但我希望它能帮助你帮助我,提前谢谢你。请原谅我的菜鸟秀。
答案 0 :(得分:0)
//在awakeFromNib方法中定义_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;
};