ios - 带图像的水平滚动 uicollectionView

标签 ios objective-c image uiscrollview uicollectionviewcell

我正在尝试在 collectionView 上显示图像。我在显示这些图像时遇到了麻烦,因为我创建了一个内部带有 imageView 的可重用单元格。这些图像之间的间距必须相等,因为我想一次在屏幕上显示 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;
 }

最佳答案

现在 OP 想要收藏 View 的水平滚动方向。所以我再次为此创建了一个小示例项目。我在水平滚动方向上放置了 13 张图像。它在收藏 View 的水平方向上成功滚动。

为什么我在这里发布水平滚动的 collectionView 是每个人都可以理解答案并轻松获得解决方案。我添加了额外的图像我的意思是正好 13 张图像(op 想要 13 张图像到 Collection View )。这个答案肯定对您有帮助。

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

enter image description here

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

现在 Storyboard设计开始

enter image description here

我的 Collection View 名称在这里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

最终输出截图

首先显示

enter image description here

然后如果我水平滚动在它显示的 Collection View 中

enter image description here

关于ios - 带图像的水平滚动 uicollectionView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42956954/

相关文章:

css - 如何调整移动设备标题图片的大小

ios - 试图在释放时加载 View Controller 的 View ... UIAlertController

objective-c - 以编程方式获取 iPhone 上的已用空间、可用空间

ios - 将掩码转换为 NSData 时保留 UIImage 上的掩码

ios - iOS 应用程序 : XIB or coding? 的自定义单元格。高度和框架位置的问题

python - 除了 python 中的文本,我如何删除图像上的所有内容?

ios - UICollectionView 右侧高度单元格重叠

ios - 通过 KeyChain 保存和检索值

ios - 如何跟踪 iPhone 应用程序安装和引荐来源网址

iphone - 在 iOS 中,使用 ARC,是否足以将所有 ivars 和属性设置为 nil,并在 viewDidUnload 中释放上下文、图像、颜色空间?