Ios 如何在集合视图中为单行仅设置两个图像
我曾尝试使用story board创建带有图像的集合视图。总共有6张图片。所以每行我需要显示2个图像。并且一行中的两个图像之间不应存在间隙。当我在所有屏幕中打开时,它应该是单行的两个图像。如何将其添加到我的代码中: ViewController.m:Ios 如何在集合视图中为单行仅设置两个图像,ios,objective-c,uicollectionview,Ios,Objective C,Uicollectionview,我曾尝试使用story board创建带有图像的集合视图。总共有6张图片。所以每行我需要显示2个图像。并且一行中的两个图像之间不应存在间隙。当我在所有屏幕中打开时,它应该是单行的两个图像。如何将其添加到我的代码中: ViewController.m: #import "ViewController.h" #import "CustomCell.h" @interface ViewController () @end @implementation ViewController {
#import "ViewController.h"
#import "CustomCell.h"
@interface ViewController ()
@end
@implementation ViewController {
NSArray *arrayImages;
NSArray *arrayLabel;
}
@synthesize MycollectionView;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[[self MycollectionView]setDataSource:self];
[[self MycollectionView]setDelegate:self];
arrayImages =[[NSArray alloc] initWithObjects:@"image1.png",@"image2.png",@"image3.png",@"image4.png",@"image5.png",@"image6.png", nil];
arrayLabel = [[NSArray alloc] initWithObjects:@"image1",@"image2",@"image3",@"image4",@"image5",@"image6", nil];
}
// datasource and delegate method
-(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";
CustomCell *Cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
[[Cell myimage] setImage:[UIImage imageNamed:[arrayImages objectAtIndex:indexPath.item]]];
[[Cell myLabel]setText:[arrayLabel objectAtIndex:indexPath.item]];
return Cell;
}
CustomCell.h
@interface CustomCell : UICollectionViewCell
@property (weak, nonatomic) IBOutlet UIImageView *myimage;
@property (weak, nonatomic) IBOutlet UILabel *myLabel;
@end
要动态设置,请使用集合视图委托方法
它会创建宽度和高度相等的集合视图单元格,即正方形如果我理解正确,您希望屏幕上只显示两个单元格吗?然后您需要设置UICollectionViewCell的大小。这就是我要问的,我们是否可以在代码中实现,或者如何在Storyboard中实现该接口,等等。。。我会贴一张图片
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
return CGSize(width: collectionView.frame.size.width/2, height: collectionView.frame.size.width/2)
}