[英]Can I integrate UIViewController and UICollectionViewController in one app?
In an app I'm making, I'm trying to mix a UIViewContoller and a UICollectionViewController in two different view controllers. 在我制作的应用程序中,我试图在两个不同的视图控制器中混合使用UIViewContoller和UICollectionViewController。 The CollectionViewController Cells won't show up in the app. CollectionViewController单元将不会显示在应用程序中。 I was wondering how I incorporate it in to my interface in the viewcontroller.h. 我想知道如何将其合并到viewcontroller.h的界面中。
Current ViewController.h 当前ViewController.h
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
@interface ViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate>
- (IBAction)cameraButtonClicked:(id)sender;
@property (strong, nonatomic) IBOutlet UIImageView *imageView;
@end
@interface ViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate, UICollectionViewDelegate, UICollectionViewDataSource>
- (IBAction)cameraButtonClicked:(id)sender;
@property (strong, nonatomic) IBOutlet UIImageView *imageView;
@property (strong, nonatomic) UICollectionView *collectionView
@end
Then, on your ViewController.m
: 然后,在您的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"];
}
Also, I think you're kind of confused on the term "View Controller" and "View." 另外,我认为您对术语“视图控制器”和“视图”感到困惑。 A UICollectionViewController
is the object you drag onto your Storyboard from the Objects Library (on your Utilities panel). UICollectionViewController
是您从“对象库”(在“实用程序”面板上)拖到故事板上的对象。 A UICollectionView
is a subclass of UIScrollView
(just like UITableView
) that incorporates a series of methods that give it it's behavior. UICollectionView
是UIScrollView
的子类(就像UITableView
一样),它合并了一系列赋予其行为的方法。
Please note that, since you're conforming to the UICollectionViewDataSource
and the UICollectionViewDelegate
protocols, you should implement the @required
methods from those protocols: 请注意,由于您符合UICollectionViewDataSource
和UICollectionViewDelegate
协议,因此@required
这些协议中实现@required
方法:
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView;
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section;
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath;
Hope I helped. 希望我能帮上忙。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.