从两个图像数组中填充2个集合视图

问题描述:

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

#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 

这里是我的cell.xib文件之一

#import <UIKit/UIKit.h> 

@interface RemotesCollectionCell : UICollectionViewCell 

@property (strong, nonatomic) IBOutlet UIImageView *remoteImageView; 


@end 

.h文件中这里是笔尖.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 

我对所有的代码表示歉意,但我希望它能帮助你,帮助我提前谢谢你。请原谅我的noob显示。

+0

您的cellForItemAtIndexPath方法需要像numberOfItemsInSection中那样的if-else子句。您需要检查哪个集合视图正在调用该方法,并将相应的单元格出列。 – rdelmar 2015-02-09 17:19:02

+0

@rdelmar感谢您的快速响应。我理解if-else部分,主要是从数组中获取图像,并将它们随机分配到单元格中。我已经发布了一个数组和一个笔尖,所以如果有人能告诉我该怎么做,我将能够弄清楚如何为其他集合视图/集合视图单元格 – Ayman 2015-02-09 17:25:00

//在awakeFromNib方法中define _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; 

      }; 
+0

做同样的事情,非常感谢。现在唯一的问题是,它不识别cellView.contentView:/ – Ayman 2015-02-09 18:25:51

+0

在awakeFromNib中定义UIImageView与标记= 1 _remoteImageView = [[UIImageView alloc] initWithFrame:self.contentView.bounds]; _remoteImageView.tag = 1; – Shashi3456643 2015-02-09 18:26:58

+0

在awakeFromNib中放置一个断点,它是否加载UIImageView?或者,您可以在界面构建器中将UIImageView拖放到collectionViewCell的contentView中,并将其标记= 1,它将起作用。 – Shashi3456643 2015-02-09 18:30:42