我想在UIView
内实现UICollectionView
,但我无法找到如何去做。有很多关于如何使用UICollectionView
和UICollectionViewController
的教程,但不知道如何在常规视图中实现它。 你是怎么做到的?UIView内的UICollectionView
回答
1)将UICollectionView
拖拽到您的UIView
中并适当调整大小。
2)创建一个属性,它也是一个IBOutlet
在.h
文件集合视图:
@property (nonatomic, retain) IBOutlet UICollectionView *myCollectionView;
3)再出现在您.h
文件中声明你的代表,所以现在你.h
应看起来somethng这样的:
@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
从那里,你可以实现尽可能多的或者根据需要尽可能少地使用方法。然而,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
需要注意的是,你可以在<UICollectionViewDelegate>
为<UICollectionViewDelegate>
替代<UICollectionViewDelegateFlowLayout>
,仍然可以访问所有的方法,因为<UICollectionViewDelegateFlowLayout>
是<UICollectionViewDelegate>
一个子类是很重要的。
尽管这个答案很有帮助,但它包含一些错误信息:1)属性的自动合成是一个编译器功能,因此不依赖于您所定位的iOS版本; 2)命名'UIViewController'子类'UtaQuickView'是不好的做法 - 它应该是'UtaQuickViewController',因为它不是视图; 3)“UICollectionViewDelegateFlowLayout”被认为符合“UICollectionViewDelegate”,它不是“子类”。对不起,如此迂腐......我知道它主要是语义,但我认为这很重要。 – Stuart 2014-01-27 11:09:39
另外,你是否有指向这两个'UICollectionViewDelegate'方法的文档链接?我正在阅读的文档显示所有方法都是可选的,实际上'UICollectionView'头文件证实了这一点。这已从iOS 6更改为7? – Stuart 2014-01-27 11:13:41
Swift的任何答案? – mobibob 2015-09-15 04:53:36
拖动一个UICollectionView到你的UIView并适当大小的。 创建一个属性,这也是您的IBOutlet。对于集合视图.h文件:
@property (nonatomic, retain) IBOutlet UICollectionView *myCollectionView;
在UIView的
首先你需要再
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
custom class *cell1=[collectionView dequeueReusableCellWithReuseIdentifier:@"identifier" forIndexPath:indexPath];
return cell1;
}
- 将UIViewController中声明你的UICollectionView细胞在
-(void)awakeFromNib
[myCollectionView registerNib:[UINib nibWithNibName:@"nib file of collectionView cell" bundle:nil] forCellWithReuseIdentifier:@"identifier"];
到stroryboard。
- 为新的uiviewcontoller创建新的类文件
- 将新创建的类设置为我们的viewcontroller。并将协议方法添加到.h文件。 -UICollectionViewDelegate,UICollectionViewDataSource
- 将uicollectionview拖到viewcontroller,默认情况下会有一个带有collectionview的uicollectionviewcell。
- 为细胞定制创建新类作为uicollectionviewcell的子类,并将该类设置为身份检查器中的单元。还要在属性检查器中设置重用标识符,我们应该指定该名称来标识uicollectionviewcell。在这里说细胞。
- 在uicollectionviewcell中拖放一个imageview(可以是任何你想要的),将其尺寸调整到适合的范围内。
- 使用imageview设置图像(该图像将与collectionview重复显示)。
- 将具有uicollectionview的委托和数据源设置为相应的viewcontroller。
设置数据源方法 - numberOfItemsInSection和cellForItemAtIndexPath。
用numberOfItemsInSection方法返回所需的单元数。在这里说10。
返回单元格以显示cellForItemAtIndexPath。
NSString * identifier = @"cell"; CollectionViewCellForDay * cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath]; return cell;
现在,你应该能够有一个视觉上有10单元集合视图:)
而斯威夫特版本
拖动一个UICollectionView到你的UIView并适当调整大小。
修改您的UIViewController延长UICollectionViewDataSource和UICollectionViewDelegate
从故事板实现所要求的功能
控制-拖动以要创建的类的出口 '的CollectionView'
在viewDidLoad()将代理和数据源连接到自身
collectionView.delegate = self
和collectionView.dataSource = self
它应该结束这样看:
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) {
}
}
- 1. UIView容器中的Swift-UICollectionView
- 2. 访问UIView内的UICollectionView单元格和视图框架(swift 3)
- 3. UICollectionView内的ViewController
- 4. UICollectionView内的UICollectionView不会滚动
- 5. UICollectionView内UICollectionViewCell
- 6. UIView内的UIButton
- 7. UIView内的UINavigationController
- 8. UICollectionVIew内的UIImageView出现无
- 9. UIView的AirPrint内容
- 10. UIView内的UIView和标签Autolayout标签
- 11. UIView内的UIView没有显示
- 12. 如何将特定的ViewController分配给UIView(在UICollectionView中)?
- 13. 如何嵌入UICollectionView内UICollectionViewSectionHeader
- 14. 处理触摸的UICollectionView内的UITableView。
- 15. 无法修改UICollectionView内的UILabel框架?
- 16. UIView背景色比UICollectionView背景色更暗
- 17. 如何在UICollectionView上添加UIView并滚动单元格?
- 18. self.removeFromSuperview()UIView子类内
- 19. 滚动的UIView其内容
- 20. UIView的内容错位
- 21. UIView内的固定键盘
- 22. UIView内部的阴影
- 23. 如何将UICollectionView的高度调整为UICollectionView内容大小的高度?
- 24. UIScrollView在主UIView内部的一个较小的UIView
- 25. UICollectionView内置IB,不显示单元格
- 26. UIView的removeFromSuperView方法是否从内存中移除UIView
- 27. UIView内的UIButton UIView无法工作后动画
- 28. UIView - 中心对齐UIView的所有内容?
- 29. UIView和内存管理
- 30. 在内部UIView(CALayerDelegate)actionForLayer
您使用的一个故事板? – jhilgert00 2013-02-14 13:11:03
是的,我正在使用故事板 – 2013-02-14 13:14:35
详细答案如下,我只是举一个简单的例子:https://github.com/lequysang/CollectionViewImages – 2013-02-16 02:02:25