2016-11-21 38 views
0

This is how it looks重新加载后,uicollectionviewcell标签未对齐

我试过以下代码在屏幕上呈现简单集合视图。 但contentview中的子视图不能正确保存

我在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 
    } 

} 
+0

您使用自动布局还是自动调整? – Vinodh

+0

我正在使用自动布局 – Anik

+0

我认为你在contraview中有冲突解决它们你将能够得到deisred output.Please张贴初始图像,看起来整齐,然后重新加载 – Vinodh

回答

1

看来,在初始呼叫时该表的第一(从ViewWillLoad)绘制的数据被使用。但是,当刷新数据时,约束不会重新计算。

通常这是通过setNeedsLayout完成和/或layoutIfNeeded 你可以试试:

[view layoutIfNeeded]; 
1

最后我得到的解决方案,自定单元

覆盖layoutSubviews

override func layoutSubviews() { 
    super.layoutSubviews() 
    self.layoutIfNeeded() 
} 

,并添加以下cellForItemAtIndexpath中的行

cell.setNeedsLayout() 
    cell.layoutIfNeeded()