我可以将UIViewController和UICollectionViewController集成到一个应用程序中吗?

问题描述:

在我做的一个应用程序中,我试图在两个不同的视图控制器中混合使用UIViewContoller和UICollectionViewController。 CollectionViewController单元格不会显示在应用程序中。我想知道如何将它整合到viewcontroller.h中的界面中。我可以将UIViewController和UICollectionViewController集成到一个应用程序中吗?

当前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 

然后,在你的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"]; 
} 

另外,我觉得你是那种对术语 “视图控制器” 和“困惑视图。” A UICollectionViewController是您从对象库(在“实用程序”面板中)拖到Storyboard上的对象。 A UICollectionViewUIScrollView的一个子类(就像UITableView),它包含一系列赋予其行为的方法。

请注意,因为你符合UICollectionViewDataSourceUICollectionViewDelegate协议,就应该从这些协议的落实@required方法:

  1. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView;
  2. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section;
  3. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath;

希望我帮了忙。