无法在swift中创建TAGVIEW?

时间:2017-12-18 12:38:13

标签: ios swift uicollectionview

我有一个集合视图。我想创建集合视图的单元格作为标签。我写过,但它不计算宽度&还留下了细胞之间的边缘。请告诉我如何改进它?

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

2 个答案:

答案 0 :(得分: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
{
}

答案 1 :(得分:0)

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

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

            return //YourCellSize here
    }
  }

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