Uicollectionview 单元格仅环绕文本

Posted

技术标签:

【中文标题】Uicollectionview 单元格仅环绕文本【英文标题】:Uicollectionview cell only wraps around text 【发布时间】:2019-12-28 06:38:42 【问题描述】:

我的 swift code collectionviewcell 在所有代码中而不是情节提要中。正如你在下面的照片中看到的那样。 uicollectionviewcell 被包裹在一个数字上,我希望在 collectionviewcell 中有更多的空间。我不知道如何使用下面的代码向 uicollectionviewcell 添加 hieght 约束。简而言之,只需将每个收集单元格设置为 100 高和 100 宽,谢谢。

import UIKit

class Cell: UICollectionViewCell 

    static var identifier: String = "Cell"

    weak var textLabel: UILabel!

    override init(frame: CGRect) 
        super.init(frame: frame)

        let textLabel = UILabel(frame: .zero)
        textLabel.translatesAutoresizingMaskIntoConstraints = false
        self.contentView.addSubview(textLabel)
        NSLayoutConstraint.activate([
            self.contentView.centerXAnchor.constraint(equalTo: textLabel.centerXAnchor),


            self.heightAnchor.constraint(equalToConstant: 100),
            self.contentView.centerYAnchor.constraint(equalTo: textLabel.centerYAnchor),

        ])
        self.textLabel = textLabel
        self.reset()
        textLabel.backgroundColor = .yellow
    

    required init?(coder aDecoder: NSCoder) 
        fatalError("init(coder:) has not been implemented")
    

    override func prepareForReuse() 
        super.prepareForReuse()
        self.reset()
    

    func reset() 
        self.textLabel.textAlignment = .center
    

class ViewController: UIViewController 

    weak var collectionView: UICollectionView!

    var data: [Int] = Array(0..<10)

    override func loadView() 
        super.loadView()

        let collectionView = UICollectionView(frame: .zero, collectionViewLayout: UICollectionViewFlowLayout())
        collectionView.translatesAutoresizingMaskIntoConstraints = false
        self.view.addSubview(collectionView)
        NSLayoutConstraint.activate([
            self.view.topAnchor.constraint(equalTo: collectionView.topAnchor),
            self.view.bottomAnchor.constraint(equalTo: collectionView.bottomAnchor),
            self.view.leadingAnchor.constraint(equalTo: collectionView.leadingAnchor),
            self.view.trailingAnchor.constraint(equalTo: collectionView.trailingAnchor),
        ])
        self.collectionView = collectionView
    

    override func viewDidLoad() 
        super.viewDidLoad()

        self.collectionView.dataSource = self
        self.collectionView.delegate = self
        self.collectionView.register(Cell.self, forCellWithReuseIdentifier: Cell.identifier)
        self.collectionView.alwaysBounceVertical = true
        self.collectionView.backgroundColor = .brown
    


extension ViewController: UICollectionViewDataSource 

    func collectionView(_ collectionView: UICollectionView,
                        numberOfItemsInSection section: Int) -> Int 
        return self.data.count
    

    func collectionView(_ collectionView: UICollectionView,
                        cellForItemAt indexPath: IndexPath) -> UICollectionViewCell 
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: Cell.identifier, for: indexPath) as! Cell
        let data = self.data[indexPath.item]
        cell.textLabel.text = String(data)


        return cell
    


extension ViewController: UICollectionViewDelegate 

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) 

    


extension ViewController: UICollectionViewDelegateFlowLayout 

    func collectionView(_ collectionView: UICollectionView,
                        layout collectionViewLayout: UICollectionViewLayout,
                        sizeForItemAt indexPath: IndexPath) -> CGSize 
        return CGSize(width: collectionView.bounds.width, height: 60)
    

    func collectionView(_ collectionView: UICollectionView,
                        layout collectionViewLayout: UICollectionViewLayout,
                        insetForSectionAt section: Int) -> UIEdgeInsets 
        return UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0) //.zero
    

    func collectionView(_ collectionView: UICollectionView,
                        layout collectionViewLayout: UICollectionViewLayout,
                        minimumInteritemSpacingForSectionAt section: Int) -> CGFloat 
        return 0
    

    func collectionView(_ collectionView: UICollectionView,
                        layout collectionViewLayout: UICollectionViewLayout,
                        minimumLineSpacingForSectionAt section: Int) -> CGFloat 
        return 0
    

【问题讨论】:

查看 UICollectionViewDelegateFlowLayout 它具有调整单元格大小的功能 ***.com/questions/54592100/… 【参考方案1】:

更新 sizeForItemAt 方法返回 w = 100 h = 100

extension ViewController: UICollectionViewDelegateFlowLayout 

    func collectionView(_ collectionView: UICollectionView,
                        layout collectionViewLayout: UICollectionViewLayout,
                        sizeForItemAt indexPath: IndexPath) -> CGSize 
        return CGSize(width: 100, height: 100)
    

【讨论】:

完全填满collectionView,返回CGSize(width: collectionView.frame.size.width, height: 100)

以上是关于Uicollectionview 单元格仅环绕文本的主要内容,如果未能解决你的问题,请参考以下文章

如何获得一个 WPF 数据网格,其中包含环绕文本而不是截断文本的单元格?

使用 iText 7 将文本环绕在单元格中的图像周围

Onclicking listview 单元格仅更改单击的单元格 xamarin 表单的内容

表视图单元格仅在屏幕外时自动布局

为啥我的自定义单元格仅在用户移开手指时突出显示?

如何使单元格仅在填充后才能读取?数据网格视图 C#