自动调整collectionview单元格显示不正确,并且行距最小

时间:2018-08-02 14:07:58

标签: ios swift3 uicollectionview autosize

我具有自动调整大小的单元格收集视图,在其中我需要水平显示数据。一切正常,但我需要在两个单元格之间放置5px的间距,因此minimumLineSpacing属性和问题开始由于行距,我的最后一个单元格无法正确显示。这是图片 enter image description here

这是代码

extension TopicVC : UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout
{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 5
    }

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

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: HastagCell.identifier, for: indexPath) as! HastagCell
        let index = indexPath.row % arrClrFollowTopic.count
        cell.btnHashTag.backgroundColor = Color.custom(hexString: arrClrFollowTopic[index], alpha: 1.0).value
        if indexPath.row % 3 == 0
        {
            cell.btnHashTag.setTitle(strTitle: "#Testing 123545")
        }
        else
        {
            cell.btnHashTag.setTitle(strTitle: "#Testing")
        }
        return cell;
    }

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

    }
}
  

ViewDidLoad

override func viewDidLoad() {
        super.viewDidLoad()


        self.objCollectionView.register(HastagCell.nib, forCellWithReuseIdentifier: HastagCell.identifier)



        if let collectionViewLayout = self.objCollectionView.collectionViewLayout as? UICollectionViewFlowLayout{
            collectionViewLayout.estimatedItemSize = CGSize.init(width: 1.0, height: 1.0)
        }

        self.objCollectionView.delegate = self
        self.objCollectionView.dataSource = self
        // Do any additional setup after loading the view.
    }
  

具有自动布局的我的单元格设计

enter image description here

  

Collectionview布局设计

enter image description here

2 个答案:

答案 0 :(得分:0)

一切都对,您只错过一件事

添加此collectionViewLayout委托方法来解决它。

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

    let label = UILabel(frame: CGRectMake(0, 0, 500, 48))

    label.text = "Your tag text"
    label.font = UIFont.boldSystemFontOfSize(14) // Set Font whatever you want
    label.sizeToFit()

    return CGSize(width: label.frame.size.width + 16, height: 40)

}

尝试此操作

cell.layoutSubviews()
cell.setNeedsLayout()
cell.layoutIfNeeded()

在返回该单元格之前,将其添加到cellForItemAt indexPath

答案 1 :(得分:-1)

您需要将布局的minimumInteritemSpacing值设置为与minimumLineSpacing相同。

因此添加:

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