@interface ViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate, UICollectionViewDelegate, UICollectionViewDataSource>
- (IBAction)cameraButtonClicked:(id)sender;
@property (strong, nonatomic) IBOutlet UIImageView *imageView;
@property (strong, nonatomic) UICollectionView *collectionView
@end
然后,在你的ViewController.m
:
- (void)viewDidload{
self.collectionView = ({
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
flowLayout.itemSize = CGSizeMake(300, 107);
flowLayout.minimumLineSpacing = 10.f;
[[UICollectionView alloc]initWithFrame:self.view.frame collectionViewLayout:flowLayout];
});
[self.collectionView setAlwaysBounceVertical:YES];
[self.collectionView setContentInset:UIEdgeInsetsMake(60, 0, 0, 0)];
self.collectionView.backgroundColor = [UIColor whiteColor];
[self.view addSubview:self.collectionView];
self.collectionView.dataSource = self;
self.collectionView.delegate = self;
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"Cell"];
}
另外,我认为您对术语“视图控制器”和“视图”有点困惑。AUICollectionViewController
是您从“对象库”(在“实用工具”面板上)拖到情节提要的对象。AUICollectionView
是UIScrollView
(就像UITableView
)的子类,它包含一系列赋予其行为的方法。
请注意,由于您符合UICollectionViewDataSource
和UICollectionViewDelegate
协议,您应该实现@required
这些协议中的方法:
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView
*)collectionView;
- (NSInteger)collectionView:(UICollectionView *)collectionView
numberOfItemsInSection:(NSInteger)section;
- (UICollectionViewCell *)collectionView:(UICollectionView
*)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath;
希望我有所帮助。