简体   繁体   English

快速,以编程方式更改UICollectionViewCell和UILabel(在单元格内)的宽度

[英]Swift, Change width of UICollectionViewCell and UILabel(inside the cell) programmatically

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. 我已经将一个单元格(UICollectionViewCell)的宽度设置为等于UICollectionView的宽度,并且我试图使用该单元格中包含的UILabel进行完全相同的操作。 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. 因此,我已经在SO和一些教程中阅读了一些问题,但是我仍然不确定如何实现这一点。

In a couple of questions it was saying about using collectionViewLayout but I'm really struggling on how to use it within my code. 在几个问题中,它是关于使用collectionViewLayout但是我真的在如何在代码中使用它方面挣扎。 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 

Update 1 更新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)
}

What happens is that when the view is loaded the UILabel's width is still small. 发生的是,在加载视图时,UILabel的宽度仍然很小。 If I go to another view and then return back then it's 100%. 如果我转到另一个视图然后返回,则为100%。 So I have to do something in the viewDidLoad() right? 所以我必须在viewDidLoad()做些事情吧? I'm already using self.collectionView.reloadData() but I guess that's only for data. 我已经在使用self.collectionView.reloadData()但是我想那仅用于数据。

Update 2 更新2

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. 在这里应该设置单元格的大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM