ios 如何以编程方式创建和使用 UIcollectionView?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27147838/
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
How to create and use UIcollectionView programmatically?
提问by Mounika Vangala
I have searched a lot for creating a UICollectionView
programatically but none of them suggest the simplest way to use it, how to add a label or an image to the UICollectionViewCell
. Most of the sites suggest that implementation of UICollectionView
is same as UITableView
, but the major difference comes when we try to add any image. In UITableView
we can allocate the imageViews in cellForRow
method where cell == nil
and assign images where (cell != nil
). but here in case of UICollectionView ItemAtIndexPath
method, there is no condition (cell == nil
) as in UITableView
's CellForRow
. As a result we can't effectively allocate variables of UImageView
s or Labels etc in itemAtIndexPath
method. I Want to know whether there is any alternative other than subclassing the UICollectionViewCell
and allocating variables in that custom Class? Can any one help, any help is appreciated.
我已经搜索了很多以UICollectionView
编程方式创建一个,但没有一个建议使用它的最简单方法,如何将标签或图像添加到UICollectionViewCell
. 大多数站点都建议 的实现与UICollectionView
相同UITableView
,但主要区别在于我们尝试添加任何图像时。在UITableView
我们可以在分配imageViewscellForRow
方法,其中,cell == nil
与分配的图像,其中(cell != nil
)。但这里在UICollectionView ItemAtIndexPath
方法的情况下,没有条件 ( cell == nil
) 中的UITableView
's CellForRow
。因此,我们无法UImageView
在itemAtIndexPath
方法中有效地分配s 或 Labels 等变量。我想知道除了子类化之外是否还有其他选择UICollectionViewCell
并在该自定义类中分配变量?任何人都可以提供帮助,任何帮助表示赞赏。
采纳答案by Mounika Vangala
There is not alternative to create or allocate cells in itemAtIndex method. We need to register the customised class to create any views inside the custom class. something like this :
在 itemAtIndex 方法中没有创建或分配单元格的替代方法。我们需要注册自定义类以在自定义类中创建任何视图。像这样:
[UICollectionView registerClass:[CustomCollectionViewClass class] forCellWithReuseIdentifier:@"cellIdentifier"];
hereis the best link which I found useful. Hope it helps others
这是我发现有用的最佳链接。希望它能帮助别人
回答by Mr. Tann
swift :
迅速:
override func viewDidLoad() {
super.viewDidLoad()
let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: 20, left: 10, bottom: 10, right: 10)
layout.itemSize = CGSize(width: 70, height: 70)
let demoCollectionView:UICollectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
demoCollectionView.dataSource = self
demoCollectionView.delegate = self
demoCollectionView.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "Cell")
demoCollectionView.backgroundColor = UIColor.whiteColor()
self.view.addSubview(demoCollectionView)
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 27
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath)
cell.backgroundColor = UIColor.lightGrayColor()
return cell
}
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath)
{
print("User tapped on item \(indexPath.row)")
}