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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 03:48:34  来源:igfitidea点击:

How to create and use UIcollectionView programmatically?

iosiphoneuicollectionview

提问by Mounika Vangala

I have searched a lot for creating a UICollectionViewprogramatically 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 UICollectionViewis same as UITableView, but the major difference comes when we try to add any image. In UITableViewwe can allocate the imageViews in cellForRowmethod where cell == niland assign images where (cell != nil). but here in case of UICollectionView ItemAtIndexPathmethod, there is no condition (cell == nil) as in UITableView's CellForRow. As a result we can't effectively allocate variables of UImageViews or Labels etc in itemAtIndexPathmethod. I Want to know whether there is any alternative other than subclassing the UICollectionViewCelland 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。因此,我们无法UImageViewitemAtIndexPath方法中有效地分配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)")
   }