【发布时间】:2017-08-24 11:23:10
【问题描述】:
尝试如下使用UICollectionView
.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UICollectionViewDataSource, UICollectionViewDelegate>
@property(strong, nonatomic) UICollectionView *collectionView;
@end
还有.m
#import "ViewController.h"
@interface ViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>
@end
@implementation ViewController
@synthesize collectionView;
- (void)viewDidLoad {
[super viewDidLoad];
self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UICollectionViewLayout *layout = [[UICollectionViewLayout alloc] init];
collectionView=[[UICollectionView alloc]initWithFrame:self.view.frame collectionViewLayout:layout];
[collectionView setDataSource:self];
[collectionView setDelegate:self];
[collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cellIdentifier"];
[collectionView setBackgroundColor:[UIColor greenColor]];
[self.view addSubview:collectionView];
}
-(NSInteger) collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return 15;
}
-(UICollectionViewCell *) collectionView:(UICollectionView *)mycollectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath
{
UICollectionViewCell *cell=[mycollectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];
cell.backgroundColor=[UIColor redColor];
return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake(50, 50);
}
@end
我打算在红色背景上显示绿色矩形列表,但只有红色背景显示,我错过了什么?
【问题讨论】:
-
确认 UICollectionViewDelegateFlowLoayout 协议。
-
提供屏幕截图。您所期待的以及即将到来的。
-
UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc] init];
-
[link](stackoverflow.com/a/17856406/5714427通过这个)
标签: ios objective-c uicollectionview