0

在我正在制作的应用程序中,我试图在两个不同的视图控制器中混合 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
4

1 回答 1

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"];
}

另外,我认为您对术语“视图控制器”和“视图”有点困惑。AUICollectionViewController是您从“对象库”(在“实用工具”面板上)拖到情节提要的对象。AUICollectionViewUIScrollView(就像UITableView)的子类,它包含一系列赋予其行为的方法。

请注意,由于您符合UICollectionViewDataSourceUICollectionViewDelegate协议,您应该实现@required这些协议中的方法:

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

希望我有所帮助。

于 2013-10-13T07:58:28.050 回答