我试图在UIView
中实现UICollectionView
,但我找不到如何实现。有很多关于如何将UICollectionView
与UICollectionViewController
一起使用的教程,但没有关于如何在常规视图中实现的教程。你是怎么做到的?
1)将UICollectionView
拖动到UIView
中,并适当调整其大小。
2)在集合视图的.h
文件中创建也是IBOutlet
的属性:
@property (nonatomic, retain) IBOutlet UICollectionView *myCollectionView;
3)再次在您的.h
文件中声明您的代理人,因此现在您的.h
应该看起来像这样:
@interface UtaQuickView : UIViewController <UICollectionViewDataSource, UICollectionViewDelegate> {
}
@property (nonatomic, retain) IBOutlet UICollectionView *myCollectionView;
4)在故事板中连接您的myCollectionView
IBOutlet
。
5)(可选)如果您的目标是iOS6以上的任何东西,请合成您的myCollectionView
属性。如果你的目标是iOS6,它会为你自动合成。这适用于所有属性,而不仅仅是UICollectionViews
。因此,在iOS6中,您根本不需要@synthesize myCollectionView = _myCollectionView
。您只需在任何需要访问该物业的地方使用_mycollectionview
即可。
6)在.m
文件viewDidLoad
中,设置delegate
和dataSource.
_myCollectionView.delegate = self;
_myCollectionView.dataSource = self;
7)实现所需的数据源方法:
#pragma mark - UICollectionView DataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
从那里,您可以根据需要实现任意多或任意少的UICollectionViewDelegate
方法。然而,根据文件,需要2个:
#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingSupplementaryView:(UICollectionReusableView *)view forElementOfKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath
需要注意的是,您可以用<UICollectionViewDelegateFlowLayout>
代替<UICollectionViewDelegate>
,并且仍然可以访问<UICollectionViewDelegate>
中的所有方法,因为<UICollectionViewDelegateFlowLayout>
是<UICollectionViewDelegate>
的子类。
UICollectionViewDataSource协议文档
UICollectionViewDelegate协议文档
和Swift版本
-
将UICollectionView拖动到UIView中,并适当调整其大小。
-
修改UIViewController以扩展UICollectionViewDataSource和UICollectionViewDelegate
-
实现所需功能
-
控制从故事板拖动到类以创建出口"collectionView"
-
在视图中,DidLoad()将委托和数据源连接到selfCCD_ 27和CCD_ 28
它最终应该是这样的:
class CustomerViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate
{
@IBOutlet weak var collectionView: UICollectionView!
override func viewDidLoad() {
collectionView.delegate = self
collectionView.dataSource = self
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
}
func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
}
func collectionView(collectionView: UICollectionView, didEndDisplayingSupplementaryView view: UICollectionReusableView, forElementOfKind elementKind: String, atIndexPath indexPath: NSIndexPath) {
}
}
将UICollectionView拖动到UIView中并适当调整其大小。在.h文件中为集合视图创建一个属性,该属性也是IBOutlet:
@property (nonatomic, retain) IBOutlet UICollectionView *myCollectionView;
在UIView中,首先需要在-(void)awakeFromNib
中声明UICollectionView单元格
[myCollectionView registerNib:[UINib nibWithNibName:@"nib file of collectionView cell" bundle:nil] forCellWithReuseIdentifier:@"identifier"];
然后
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
custom class *cell1=[collectionView dequeueReusableCellWithReuseIdentifier:@"identifier" forIndexPath:indexPath];
return cell1;
}
- 将uiviewcontroller拖到存储板上
- 为新的uiviewcontoller创建新的类文件
- 将新创建的类设置为我们的视图控制器。并将协议方法添加到.h文件中-UICollectionViewDelegate,UICollectionViewDataSource
- 将uicollectionview拖动到视图控制器中,默认情况下会有一个带有collectionview的uicollectionviewcell
- 为单元格自定义创建新的类作为uicollectionviewcell的子类,并将该类设置为标识检查器中的单元格。还要在属性检查器中设置重用标识符,我们应该指定该名称来标识uicollectionviewcell。在这里说cell
- 将图像视图(可以是任何您想要的)拖放到uicollectionview单元格中,调整其大小以适合其中
- 使用imageview设置图像(此图像将与collectionview一起重复显示)
- 将具有uicollectionview的委托和数据源设置为相应的视图控制器
-
设置数据源方法-numberOfItemsInSection和cellForItemAtIndexPath。
-
使用numberOfItemsSection方法返回所需的单元格计数。在这里说10。
-
返回要与cellForItemAtIndexPath一起显示的单元格。
NSString * identifier = @"cell"; CollectionViewCellForDay * cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath]; return cell;
到目前为止,您应该能够看到10个单元格的集合视图:)