xcode UIView 里面的 UICollectionView
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14874320/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UICollectionView inside UIView
提问by Matias Frank Jensen
I am trying to implement a UICollectionView
inside a UIView
, but I cant find out how to do it. There is a lot of tutorials on how to use UICollectionView
with a UICollectionViewController
, but not how to implement one in a regular View.
How do you do that?
我正在尝试在 aUICollectionView
内部实现一个UIView
,但我不知道如何去做。有关于如何使用大量的教程UICollectionView
有UICollectionViewController
,但不知道如何实现一个在普通视图。你是怎样做的?
回答by jhilgert00
1)Drag a UICollectionView
into your UIView
and size it appropriately.
1)将 aUICollectionView
拖入您的UIView
并适当调整大小。
2)Create a property which is also an IBOutlet
in your .h
file for the collection view:
2)IBOutlet
在您的.h
文件中为集合视图创建一个属性:
@property (nonatomic, retain) IBOutlet UICollectionView *myCollectionView;
3)Again in your .h
file declare your delegates, so now your .h
should look somethng like this:
3)再次在您的.h
文件中声明您的委托,所以现在您.h
应该看起来像这样:
@interface UtaQuickView : UIViewController <UICollectionViewDataSource, UICollectionViewDelegate> {
}
@property (nonatomic, retain) IBOutlet UICollectionView *myCollectionView;
4)Connect your myCollectionView
IBOutlet
in your storyboard.
4)将您myCollectionView
IBOutlet
的故事板连接到您的故事板中。
5)(optional) If you're targeting anything older than iOS6 synthesize your myCollectionView
property. If you're targeting iOS6, it will auto-synthesize it for you. This goes for all properties, not just UICollectionViews
. So in iOS6, you don't need to @synthesize myCollectionView = _myCollectionView
at all. You can just use _mycollectionview
wherever you need to access the property.
5)(可选)如果您的目标是 iOS6 之前的任何内容,请综合您的myCollectionView
属性。如果您的目标是 iOS6,它会为您自动合成。这适用于所有属性,而不仅仅是UICollectionViews
. 所以在iOS6中,你根本不需要@synthesize myCollectionView = _myCollectionView
。您可以_mycollectionview
在需要访问该属性的任何地方使用。
6)In your .m
file viewDidLoad
, set your delegate
and dataSource.
6)在您的.m
文件中viewDidLoad
,设置您的delegate
和dataSource.
_myCollectionView.delegate = self;
_myCollectionView.dataSource = self;
7)Implement the required dataSource methods:
7)实现所需的 dataSource 方法:
#pragma mark - UICollectionView DataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
From there you can implement as many or as little of the UICollectionViewDelegate
methods as you need. However, 2 are required according to the documentation:
从那里您可以根据需要实现尽可能多或尽可能少的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
It's important to note that you can substitute <UICollectionViewDelegateFlowLayout>
for <UICollectionViewDelegate>
and still have access to all of the methods in <UICollectionViewDelegate>
because <UICollectionViewDelegateFlowLayout>
is a subclass of <UICollectionViewDelegate>
.
这是要注意的是可以替代的重要<UICollectionViewDelegateFlowLayout>
的<UICollectionViewDelegate>
,仍然可以访问所有的方法<UICollectionViewDelegate>
,因为<UICollectionViewDelegateFlowLayout>
是的子类<UICollectionViewDelegate>
。
UICollectionViewDataSource Protocol Documentation
UICollectionViewDataSource 协议文档
回答by Michael Hudson
And the Swift version
和 Swift 版本
Drag a UICollectionView into your UIView and size it appropriately.
Modify your UIViewController to extend UICollectionViewDataSource and UICollectionViewDelegate
Implement the required functions
Control-Drag from your storyboard to the class to create an outlet 'collectionView'
In the viewDidLoad() wire up the delegate and datasource to self
collectionView.delegate = self
andcollectionView.dataSource = self
将 UICollectionView 拖入 UIView 并适当调整大小。
修改您的 UIViewController 以扩展 UICollectionViewDataSource 和 UICollectionViewDelegate
实现所需的功能
控制并从故事板拖动到类以创建出口“collectionView”
在 viewDidLoad() 中将委托和数据源连接到 self
collectionView.delegate = self
和collectionView.dataSource = self
It should end up looking like this:
它最终应该是这样的:
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) {
}
}
回答by Hilaj
Drag a UICollectionView into your UIView and size it appropriately. Create a property which is also an IBOutlet in your .h file for the collection view:
将 UICollectionView 拖入 UIView 并适当调整大小。在您的 .h 文件中为集合视图创建一个属性,它也是一个 IBOutlet:
@property (nonatomic, retain) IBOutlet UICollectionView *myCollectionView;
in UIView first you need to declare your UICollectionView cell in -(void)awakeFromNib
首先在 UIView 你需要声明你的 UICollectionView 单元格 -(void)awakeFromNib
[myCollectionView registerNib:[UINib nibWithNibName:@"nib file of collectionView cell" bundle:nil] forCellWithReuseIdentifier:@"identifier"];
then
然后
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
custom class *cell1=[collectionView dequeueReusableCellWithReuseIdentifier:@"identifier" forIndexPath:indexPath];
return cell1;
}
回答by Tony
- Drag uiviewcontroller to the stroryboard.
- Create the new class file for the new uiviewcontoller
- Set the newly created class to our viewcontroller. And add protocol methods to the .h file. -UICollectionViewDelegate,UICollectionViewDataSource
- Drag uicollectionview to the viewcontroller, by default there will be a uicollectionviewcell with the collectionview.
- Create new class for the cell customisation as the subclass of the uicollectionviewcell and set the class to the cell in the identity inspector. Also set reuse identifier in the attributes inspector, the name we should specify to identify the uicollectionviewcell. Say cell here.
- Drag and drop an imageview(can be anything as your wish) inside the uicollectionviewcell, size it to fit within.
- Set an image with the imageview(this image will be shown repeatedly with the collectionview).
- Set the delegate and datasource with the uicollectionview to the corresponding viewcontroller.
Set datasource methods - numberOfItemsInSection and cellForItemAtIndexPath.
Return the required cell count with the numberOfItemsInSection method. Say 10 here.
Return the cell to display with the cellForItemAtIndexPath.
NSString * identifier = @"cell"; CollectionViewCellForDay * cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath]; return cell;
- 将 uiviewcontroller 拖到故事板。
- 为新的 uiviewcontoller 创建新的类文件
- 将新创建的类设置为我们的视图控制器。并将协议方法添加到 .h 文件中。-UICollectionViewDelegate,UICollectionViewDataSource
- 将 uicollectionview 拖到 viewcontroller 中,默认会有一个带有 collectionview 的 uicollectionviewcell。
- 为单元格自定义创建新类作为 uicollectionviewcell 的子类,并将类设置为身份检查器中的单元格。还要在属性检查器中设置重用标识符,我们应该指定该名称来标识 uicollectionviewcell。在这里说单元格。
- 在 uicollectionviewcell 中拖放一个图像视图(可以是你想要的任何东西),调整它的大小以适应它。
- 使用 imageview 设置图像(此图像将与 collectionview 重复显示)。
- 将带有 uicollectionview 的委托和数据源设置为相应的视图控制器。
设置数据源方法 - numberOfItemsInSection 和 cellForItemAtIndexPath。
使用 numberOfItemsInSection 方法返回所需的单元格计数。在这里说 10。
使用 cellForItemAtIndexPath 返回要显示的单元格。
NSString * identifier = @"cell"; CollectionViewCellForDay * cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath]; return cell;
By now, you should be able to have a visual of a 10 cell collection view :)
到现在为止,您应该能够看到 10 个单元格集合视图:)