我尝试使用以下代码在屏幕上呈现简单的集合视图。
但是contentview中的子视图不能正确地进行algin
我用swift 2.3和Xcode 7.3.1编码
为什么会这样?
这是密码

import Foundation
class CustomerOverdueViewController : BaseViewController , UICollectionViewDelegateFlowLayout {

    @IBOutlet weak var collectionView: UICollectionView!
    @IBOutlet weak var tableView: UITableView!

    @IBOutlet weak var lblTotalOverdue: UILabel!
    @IBOutlet weak var lblInvestedFunds: UILabel!

    @IBOutlet weak var segmentedControl: UISegmentedControl!

    var rangeArray = NSArray()
    var selectedOverdueIndex = 0
    var selectedColor = UIColor()

    override func viewDidLoad() {
        super.viewDidLoad()

        self.title = "Customer Overdue"

    }


    @IBAction func segValueChanged(sender: UISegmentedControl) {

    }

    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return 1
    }

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 6
    }

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

        let reuseIdentifier = String("CustomerOverdueCollectionViewCell")
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! CustomerOverdueCollectionViewCell
        return cell
    }

    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath){
    }


    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize
    {
        return CGSize(width: collectionView.frame.size.width/2 - 10, height: collectionView.frame.size.height/3 - 10)
    }

    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets
    {
        return UIEdgeInsets(top: 5, left: 5, bottom: 0, right: 5)
    }
    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAtIndex section: Int) -> CGFloat
    {
        return 10.0
    }
    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAtIndex section: Int) -> CGFloat
    {
        return 10.0
    }

}

最佳答案

在初始调用中,数据似乎是在首次绘制表时使用的(从ViewWillLoad)。但是,刷新数据时,不会重新计算约束。
通常这是通过setNeedsLayout和/或layoutIfNeeded完成的
您可以尝试:

[view layoutIfNeeded];

关于ios - uicollectionviewcell标签在重新加载后未对齐,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40718412/

10-09 18:09
查看更多