Swift,以编程方式更改UICollectionViewCell和UILabel(在单元格内)的宽度 [英] Swift, Change width of UICollectionViewCell and UILabel(inside the cell) programmatically
问题描述
我已将单元格的宽度(UICollectionViewCell)设置为等于UICollectionView的宽度,并且我尝试使用该单元格中包含的UILabel执行完全相同的操作。我认为下面的代码正好解释了我想要实现的目标。所以我在这里阅读了一些问题以及一些教程,但我仍然不确定如何实现这一点。
I've set the width of a cell(UICollectionViewCell) to be equal to the width of the UICollectionView and I'm trying to do exactly the same thing with the UILabel that is included inside that cell. I think the code below explains exactly what I'm trying to achieve. So i've read some question here in SO and also a couple of tutorials but I'm still not sure how I can achieve this.
在几个问题中它关于使用 collectionViewLayout
的说法,但我真的在如何在我的代码中使用它。有任何想法吗?谢谢!
In a couple of questions it was saying about using collectionViewLayout
but I'm really struggling on how to use it within my code. Any ideas? Thank you!
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
var cell = collectionView.dequeueReusableCellWithReuseIdentifier("myCell", forIndexPath: indexPath) as LocationViewCell
cell.locationLabel.text = "Hello there!"
// Set cell & label width to 100%
let collectionViewWidth = self.collectionView.bounds.size.width
cell.frame.size.width = collectionViewWidth // Works
cell.locationLabel.frame.size.width = collectionViewWidth // Does NOT
更新1
所以我添加了以下内容:
Update 1
So I added the following:
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
// Set cell width to 100%
let collectionViewWidth = self.collectionView.bounds.size.width
return CGSize(width: collectionViewWidth, height: 35)
}
当加载视图时,UILabel的宽度仍然很小。如果我去另一个视图,然后返回,那么它是100%。所以我必须在 viewDidLoad()
中做点什么吧?我已经在使用 self.collectionView.reloadData()
但我猜这只是针对数据。
What happens is that when the view is loaded the UILabel's width is still small. If I go to another view and then return back then it's 100%. So I have to do something in the viewDidLoad()
right? I'm already using self.collectionView.reloadData()
but I guess that's only for data.
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
var cell = collectionView.dequeueReusableCellWithReuseIdentifier("locationCell", forIndexPath: indexPath) as LocationViewCell
cell.locationLabel.text = "Hello UILabel"
// Set cell width to 100%
let collectionViewWidth = self.collectionView.bounds.size.width
cell.frame.size.width = collectionViewWidth
cell.locationLabel.frame.size.width = collectionViewWidth
return cell
}
推荐答案
它不起作用,因为在调用此方法时,集合视图已经知道单元格应该有多大,因为它已从流委托方法获取它:
It doesn't work because by the time this method is called, the collection view already knows how big the cell should be because it has got it from the flow delegate method:
optional func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize
这是你应该设置单元格大小的地方。
This is where you should be setting the size of your cells.
这篇关于Swift,以编程方式更改UICollectionViewCell和UILabel(在单元格内)的宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!