代码之家  ›  专栏  ›  技术社区  ›  Chirag Shah

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

  •  0
  • Chirag Shah  · 技术社区  · 6 年前

    我有autosize cell collection视图,需要在其中水平显示数据。一切正常,但我需要在两个电池之间设置5倍的间距 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) {
    
        }
    }
    

    视图加载

    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

    集合视图布局设计

    enter image description here

    1 回复  |  直到 6 年前
        1
  •  0
  •   Nikunj Kumbhani    6 年前

    一切都是对的你只错过了一件事

    添加此 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 回牢房前

        2
  •  -1
  •   Anthony    4 年前

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

    所以补充:

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