问题而收集观察室设置宽动态

问题描述 投票:0回答:3

我想动态设置集合视图单元格的宽度。最初,它不是渲染预期。但是当我在电池接头,其得到调整我想。下面是我写的代码:

import UIKit

class ViewController: UIViewController,UICollectionViewDelegate,UICollectionViewDataSource {

    @IBOutlet weak var collView: UICollectionView!


    var tasksArray = ["To Do", "SHOPPING","WORK"]
    var selectedIndex = Int()

    override func viewDidLoad() {
        super.viewDidLoad()
        let layout = collView?.collectionViewLayout as! UICollectionViewFlowLayout
        layout.itemSize = UICollectionViewFlowLayout.automaticSize
        layout.estimatedItemSize = CGSize(width: 93, height: 40)
        // Do any additional setup after loading the view, typically from a nib.
    }
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return tasksArray.count
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! CollectionViewCell
        cell.lblName.text = tasksArray[indexPath.row]
        if selectedIndex == indexPath.row
        {
            cell.backgroundColor = UIColor.lightGray
        }
        else
        {
            cell.backgroundColor = UIColor.white
        }
        cell.layer.borderWidth = 1
        cell.layer.cornerRadius = cell.frame.height / 2
        return cell
    }
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        selectedIndex = indexPath.row
        self.collView.reloadData()
    }
}

在这里,我出钢前将两个图像和窃听等你以后可以很容易理解

[![这里是我的形象挖掘之前

qazxsw对细胞] qazxsw POI] qazxsw POI的POI

所以请告诉我,什么是错在我的代码

ios swift uicollectionview uicollectionviewdelegateflowlayout
3个回答
1
投票

里面你this is after tapping on cell its give me perfect result覆盖2功能这就是细胞有机会表明其首选属性,包括大小,我们计算使用自动布局。

2

1
投票

我已经找到了SWIFT 4.2个小窍门

对于动态宽度与固定高度:

CollectionViewCell

0
投票

很明显,你必须为了通过动态宽度使用preferredLayoutAttributesFitting流布局委托。但棘手的部分是为基于文本的单元格的宽度。实际上,你可以计算出给定的,你有一个字体的文本的宽度。

让我们介绍几个扩展,这将有助于我们前进的道路上

StringExtensions.swift

 override func preferredLayoutAttributesFitting(_ layoutAttributes: UICollectionViewLayoutAttributes) -> UICollectionViewLayoutAttributes {
    setNeedsLayout()
    layoutIfNeeded()
    let size = contentView.systemLayoutSizeFitting(layoutAttributes.size)
    var frame = layoutAttributes.frame
    frame.size.width = ceil(size.width)
    layoutAttributes.frame = frame
    return layoutAttributes
}

这种方法让我们知道一个字符串的宽度,如果我提供它的高度和字体。然后按如下方式使用它里面func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { let label = UILabel(frame: CGRect.zero) label.text = textArray[indexPath.item] label.sizeToFit() return CGSize(width: label.frame.width, height: 32) }

sizeForItemAt
© www.soinside.com 2019 - 2024. All rights reserved.