ios - 重新加载后 uicollectionviewcell 标签未对齐

标签 ios swift uicollectionview

This is how it looks

我尝试使用以下代码在屏幕上呈现简单的 Collection View 。 但是 contentview 中的 subview 不能正常生成

我正在使用 Xcode 7.3.1 在 swift 2.3 中编码 为什么会这样?

这是代码

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
    }

}

最佳答案

终于找到答案了

覆盖自定义单元格中的布局 subview

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

并在 cellForItemAtIndexpath 中添加以下行

    cell.setNeedsLayout()
    cell.layoutIfNeeded()

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

相关文章:

ios - 关闭应用程序时 webview 的 cookie 会发生什么变化?

ios - 是否可以从 watchOS 3 应用程序唤醒 iPhone 应用程序?

ios - 你如何从 Swift 或 Objective C 中的 UIImage 获取 URL?

ios - 选择 UICollectionView 中的单元格时调整图像大小

ios - 为 CollectionView 的单独 Cell 设置圆形标签 [Swift]

ios - 如何将场景切换到导航 Controller 堆栈的几层?

ios - SWRevealViewController 如何修复 popoViewController 的工作?

ios - 不从缓存中获取对象 - 解析

Swift:设备横向启动时约束不正确

swift - 尝试为 UICollectionView Swift 添加 header 时出错