无法在swift中创建TAGVIEW?

问题描述 投票:1回答:2

我有一个集合视图。我想创建集合视图的单元格作为标签。我已经写过,但它没有计算宽度,也没有计算出细胞之间的宽度。请告诉我如何改进它?

extension StoreItemCell:UICollectionViewDelegate {
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {

        let width = CGSize(
        return self.sizingCell!.systemLayoutSizeFitting(UILayoutFittingCompressedSize)
    }        
}



 override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        self.tagsCollectionView.delegate = self
        self.tagsCollectionView.dataSource = self
        let cellNib = UINib(nibName: Titles.CellIdentifier.TagCell, bundle: nil)
        self.tagsCollectionView.register(cellNib, forCellWithReuseIdentifier: Titles.CellIdentifier.TagCell)
        self.tagsCollectionView.backgroundColor = UIColor.clear
        self.sizingCell = (cellNib.instantiate(withOwner: nil, options: nil) as NSArray).firstObject as! TagCell?
        self.flowLayout.sectionInset = UIEdgeInsetsMake(8, 8, 8, 8)
        self.flowLayout.minimumInteritemSpacing = 15
    }

请告诉我如何改进它。我附上了我的设计截图。

enter image description here

ios swift uicollectionview
2个回答
1
投票

添加这个

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let Labell : UILabel = UILabel()

        Labell.text =   self.TagsArray[indexPath.item] as? String
        let labelTextWidth =   Labell.intrinsicContentSize.width
        return CGSize(width: labelTextWidth + 10, height: 30)


    }

创建UILabel

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{
}

0
投票

您的代码应该在您的父类中,而不是在单元类中,

extension YourViewController:UICollectionViewDelegate { 
    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {

            return //YourCellSize here
    }
  }

您必须在父类中使用该委托方法。不在细胞类中。

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