ios - 水平滚动带图像的 collectionView

ios - horizontally scrolling collectionView with images

我正在尝试在 collectionView 上显示图像。我在显示这些图像时遇到了问题,因为我创建了一个带有 imageView inside.Those 的可重用单元格,图像之间的间距必须相等,因为我想一次在屏幕上显示 3 个图标。我正在使用 13 个图标,它必须可以在屏幕上水平滚动。

CustomCollectionViewCell.h

@interface CustomCollectionViewCell : UICollectionViewCell
@property (nonatomic, retain) UIImageView *imageView;

@end

CustomCollectionViewCell.m

   @implementation CustomCollectionViewCell

    - (UIImageView *) imageView {

   if (!_imageView) {
      _imageView = [[UIImageView alloc]      initWithFrame:self.contentView.bounds];
    [self.contentView addSubview:_imageView];
}

return _imageView;
   }

   - (void)prepareForReuse {

    [super prepareForReuse];

[self.imageView removeFromSuperview];
self.imageView = nil;
}
@end

LandingViewController.m

   - (UICollectionViewCell *)collectionView:(UICollectionView *)cv     cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath {

   CustomCollectionViewCell *cell = [cv    dequeueReusableCellWithReuseIdentifier:@"CustomCell" forIndexPath:indexPath];

return cell;
 }

有求必应

我想把同样的事情合二为一 application.I 成功实施 this.Just 现在我尝试了您的示例项目 question.I 得到了您的要求 exactly.I 给您看代码以及下面的所有内容。

NSObject 的第一个 CustomImageFlowLayout Class

CustomImageFlowLayout.h

#import <UIKit/UIKit.h>
@interface CustomImageFlowLayout : UICollectionViewFlowLayout
@end

CustomImageFlowLayout.m

#import "CustomImageFlowLayout.h"

@implementation CustomImageFlowLayout

- (instancetype)init
{
    self = [super init];
    if (self)
    {
        self.minimumLineSpacing = 1.0f;
        self.minimumInteritemSpacing = 1.0f;
        self.scrollDirection = UICollectionViewScrollDirectionVertical;
    }
    return self;
}

- (CGSize)itemSize
{
    NSInteger numberOfColumns;
    if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
       numberOfColumns = 3;
    else{
        if([UIApplication sharedApplication].statusBarOrientation==UIInterfaceOrientationPortrait)
            numberOfColumns = 4;
        else if([UIApplication sharedApplication].statusBarOrientation == UIDeviceOrientationLandscapeRight || [UIApplication sharedApplication].statusBarOrientation == UIDeviceOrientationLandscapeLeft)
            numberOfColumns = 4;
    }
    NSLog(@"The collection view frame is - %@",NSStringFromCGRect(self.collectionView.frame));
    CGFloat itemWidth = (CGRectGetWidth(self.collectionView.frame) - (numberOfColumns - 1)) / numberOfColumns;
    NSLog(@"The item width is - %f",itemWidth);
    return CGSizeMake(itemWidth, itemWidth);
}

@end

之后我为图像创建了自定义单元格

先看看我的设计

CustomCell.h

#import <UIKit/UIKit.h>
@interface CustomCell : UICollectionViewCell
@property (strong, nonatomic) IBOutlet UIImageView *img;
@property (strong, nonatomic) IBOutlet UILabel *lblCollection;
@end

CustomCell.m

#import "CustomCell.h"
@implementation CustomCell
@end

然后我在ViewController

中使用上面的class

下面是我的故事板设计

这里我的 CollectionView 名称是 collectionViewVertical

ViewController.h

#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
@property (strong, nonatomic) IBOutlet UICollectionView *collectionViewVertical;
@end

ViewController.m

#import "ViewController.h"
#import "CustomCell.h"
#import "CustomImageFlowLayout.h"
@interface ViewController ()
{
    NSMutableArray *arrayImages;
    NSMutableArray *arrayTitles;
    CustomCell *cell;
}
@end

@implementation ViewController

@synthesize collectionViewVertical;

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    collectionViewVertical.collectionViewLayout = [[CustomImageFlowLayout alloc] init];
    collectionViewVertical.backgroundColor = [UIColor clearColor];

    arrayImages = [[NSMutableArray alloc]initWithObjects:@"iPhone", @"Android", @"Windows", @"Blackberry", @"Lenova", @"Redmi", @"MotoG", @"Sony", @"Samsung", @"OnePlus", nil];
    arrayTitles = [[NSMutableArray alloc]initWithObjects:@"iPhone.png", @"android.png", @"windows.png", @"blackberry.png", @"lenovo.png", @"redmi.png", @"moto.png", @"sony.png", @"samsung.png", @"oneplus.png", nil];

    UINib *cellNib = [UINib nibWithNibName:@"CustomCell" bundle:nil];
    [collectionViewVertical registerNib:cellNib forCellWithReuseIdentifier:@"cell"];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//UICollectionView Data Source Methods
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return  1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return arrayImages.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"cell";
    cell = (CustomCell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
    if(cell==nil){
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
        cell = nib[0];
    }
    cell.img.image = [UIImage imageNamed:(NSString*)[arrayImages objectAtIndex:indexPath.row]];
    cell.lblCollection.text = arrayTitles[indexPath.row];

    return cell;
}

//UICollectionView Delegate Method
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"the clicked indexPath.row is - %ld",(long)indexPath.row);
}

终于输出画面了

现在 OP 想要 Collection 的水平滚动方向 view.So 我再次为 this.I 创建了一个小示例项目,将 13 个图像水平滚动 direction.It 在水平方向上成功滚动collection 视图。

为什么我post水平滚动collection查看这里是每个人都可以理解的答案并得到解决方案easily.I添加额外的图像我的意思是正好13张图片(op想要13张图片进入 collection 视图)。这个答案绝对对你有帮助。

Horizo​​ntalScrollableCollectionView

这里我在CustomImageFlowLayout.m文件

中将scrollDirection设置为Horizo​​ntal

CustomImageFlowLayout.h

#import <UIKit/UIKit.h>
@interface CustomImageFlowLayout : UICollectionViewFlowLayout
@end

CustomImageFlowLayout.m

#import "CustomImageFlowLayout.h"

@implementation CustomImageFlowLayout

- (instancetype)init
{
    self = [super init];
    if (self)
    {
        self.minimumLineSpacing = 1.0f;
        self.minimumInteritemSpacing = 1.0f;
        self.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    }
    return self;
}

- (CGSize)itemSize
{
    NSInteger numberOfColumns;
    if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
       numberOfColumns = 3;
    else{
        if([UIApplication sharedApplication].statusBarOrientation==UIInterfaceOrientationPortrait)
            numberOfColumns = 4;
        else if([UIApplication sharedApplication].statusBarOrientation == UIDeviceOrientationLandscapeRight || [UIApplication sharedApplication].statusBarOrientation == UIDeviceOrientationLandscapeLeft)
            numberOfColumns = 4;
    }
    NSLog(@"The collection view frame is - %@",NSStringFromCGRect(self.collectionView.frame));
    CGFloat itemWidth = (CGRectGetWidth(self.collectionView.frame) - (numberOfColumns - 1)) / numberOfColumns;
    NSLog(@"The item width is - %f",itemWidth);
    return CGSizeMake(itemWidth, itemWidth);
}

@end

然后是UICollectionViewCell

的CustomCell

CustomCell.xib

CustomCell.h

#import <UIKit/UIKit.h>
@interface CustomCell : UICollectionViewCell
@property (strong, nonatomic) IBOutlet UIImageView *img;
@property (strong, nonatomic) IBOutlet UILabel *lblCollection;
@end

CustomCell.m

#import "CustomCell.h"
@implementation CustomCell
- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}
@end

现在故事板设计开始

我的 collection 视图名称在这里 collectionviewVerticalHorizo​​ntalFlowLayout

ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UICollectionViewDelegate,UICollectionViewDataSource>

@property (strong, nonatomic) IBOutlet UICollectionView *collectionviewVerticalHorizontalFlowLayout;

@end

ViewController.m

#import "ViewController.h"
#import "CustomCell.h"
#import "CustomImageFlowLayout.h"


@interface ViewController (){
    NSMutableArray *arrayImages;
    NSMutableArray *arrayTitles;
    CustomCell *cell;
}

@end

@implementation ViewController

@synthesize collectionviewVerticalHorizontalFlowLayout;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    collectionviewVerticalHorizontalFlowLayout.collectionViewLayout = [[CustomImageFlowLayout alloc] init];
    collectionviewVerticalHorizontalFlowLayout.backgroundColor = [UIColor clearColor];

    arrayImages = [[NSMutableArray alloc]initWithObjects:@"iPhone.png", @"android.png", @"windows.png", @"blackberry.png", @"lenovovibek5note.png", @"redmi.png", @"moto.png", @"sony.png", @"samsung.png", @"oneplus.png",@"redminote4.png",@"oppo.png",@"vivo.png",nil];
    arrayTitles = [[NSMutableArray alloc]initWithObjects:@"iPhone", @"Android", @"Windows", @"Blackberry", @"LenovaVikeK5Note", @"Redmi", @"MotoG", @"Sony", @"Samsung", @"OnePlus", @"RedMiNote4",@"Oppo",@"Vivo",nil];

    UINib *cellNib = [UINib nibWithNibName:@"CustomCell" bundle:nil];
    [collectionviewVerticalHorizontalFlowLayout registerNib:cellNib forCellWithReuseIdentifier:@"cell"];

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//UICollectionView Data Source Methods
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return  1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return arrayImages.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"cell";
    cell = (CustomCell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
    if(cell==nil){
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
        cell = nib[0];
    }
    cell.img.image = [UIImage imageNamed:(NSString*)[arrayImages objectAtIndex:indexPath.row]];
    NSLog(@"The collection view label text is - %@",[NSString stringWithFormat:@"%@",arrayTitles[indexPath.row]]);
    cell.lblCollection.text = arrayTitles[indexPath.row];
    cell.lblCollection.textColor = [UIColor blackColor];
    return cell;
}

//UICollectionView Delegate Method
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"the clicked indexPath.row is - %ld",(long)indexPath.row);
}


@end

最终输出截图

首先显示

然后 如果我在 collection 视图中水平滚动 它会显示