簡體   English   中英

從兩個圖像陣列填充2個集合視圖

[英]Populating 2 collection views from two arrays of images

所以我一直在搜索,盡管有很多線程在解釋接近我所需的內容,但我仍然無法成功執行我的代碼。 我的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

這是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

對於所有代碼,我都表示歉意,但希望能對您有所幫助,在此先感謝您。 請原諒我的菜鳥表演。

//在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;

            };

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM