【问题标题】:Can I integrate UIViewController and UICollectionViewController in one app?我可以在一个应用程序中集成 UIViewController 和 UICollectionViewController 吗?
【发布时间】:2013-10-13 00:16:07
【问题描述】:

在我正在制作的应用程序中,我试图在两个不同的视图控制器中混合 UIViewContoller 和 UICollectionViewController。 CollectionViewController 单元不会显示在应用程序中。我想知道如何将它合并到 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

【问题讨论】:

    标签: ios objective-c ios7 xcode5 iphone-developer-program


    【解决方案1】:
    @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"];
    }
    

    另外,我认为您对术语“视图控制器”和“视图”有些混淆。 UICollectionViewController 是您从“对象库”(在“实用工具”面板上)拖到故事板上的对象。 UICollectionView 是UIScrollView 的子类(就像UITableView),它结合了一系列赋予其行为的方法。

    请注意,由于您符合UICollectionViewDataSource 和UICollectionViewDelegate 协议,您应该从这些协议中实现@required 方法:

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

    希望我能帮上忙。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多