【问题标题】:CollectionView subview not showingCollectionView 子视图未显示
【发布时间】:2021-03-08 22:10:59
【问题描述】:

我想将this ring progress view (CoocaPod) 放入 CollectionView 单元格中。一个进度视图位于一个单元格中。不知何故,环形进度视图没有显示......这是我的代码:

CollectionView 控制器类:

import UIKit

private let reuseIdentifier = "Cell"

class myCollectionViewController: UICollectionViewController {

    var cellColor = true
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.collectionView!.register(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
    }


    override func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }


    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of items
        return 19
    }

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath)
    

        cell.backgroundColor = cellColor ? UIColor.red : UIColor.blue
        cellColor = !cellColor
        
        return cell
    }

}

CollectionView 单元格类:

import UIKit
import MKRingProgressView

class myCollectionViewCell: UICollectionViewCell {
    @IBOutlet weak var ringView: UIView!
    
    override func layoutSubviews() {
        let ringProgressView = RingProgressView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
        ringProgressView.startColor = .red
        ringProgressView.endColor = .magenta
        ringProgressView.ringWidth = 5
        ringProgressView.progress = 1.0
        ringView.addSubview(ringProgressView)
    }
}

我在 Main.storyboard 中为 Cell 选择了 myCollectionViewCell 类,为 ViewController 选择了 myCollectionViewController

有人可以帮助我吗? 谢谢!

【问题讨论】:

  • [有人告诉我将 UICollectionView.self 更改为我注册单元格的 myCollectionViewCell.self,然后删除了他的评论]谢谢!但随后它在单元格类的最后一行( ringView.addSubview(ringProgressView) )给了我一个“线程 1:致命错误:在隐式展开可选值时意外发现 nil”
  • Ring View 是 Cell 内的 UIView,应该包含进度圈
  • 啊,你直接在情节提要中这样做了,我认为你不需要注册任何东西。只需确保在情节提要中将重用标识符设置为 Cell
  • 谢谢!取消注释后现在看起来不错

标签: ios swift xcode uicollectionview


【解决方案1】:

取消注释注册行。现在工作

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多