CollectionView中的单元格未显示

时间:2017-12-10 11:03:54

标签: ios swift

 我试图以编程方式创建一个UICollectionView。这就是我到目前为止所拥有的

我还包含了UICollectionViewController,UICollectionViewDelegateFlowLayout

    override func viewDidLoad() {
    super.viewDidLoad()
    self.collectionView!.backgroundColor = UIColor(red: 153/255, green: 204/255, blue: 153/255, alpha: 1)

    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = false
    self.collectionView!.alwaysBounceVertical = true
    // Register cell classes
    self.collectionView!.register(threadCell.self, forCellWithReuseIdentifier: reuseIdentifier)


    // Do any additional setup after loading the view.
}

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

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

    // Configure the cell

    return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    return CGSize(width: view.frame.height, height: 50)
}

    class threadCell: UICollectionViewCell{
    override init(frame: CGRect){
        super.init(frame: frame)
        setupViews()
    }
    let userName: UILabel = {
        let name = UILabel()
        name.text = "testing"
        name.font = UIFont.boldSystemFont(ofSize: 14)
        name.translatesAutoresizingMaskIntoConstraints = false
        return name

    }()
    required init?(coder aDecoder: NSCoder) {
        fatalError()
    }
    func setupViews(){
        backgroundColor = UIColor.blue
        addSubview(userName)
        addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[v0]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0" : userName]))
        addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[v0]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0" : userName]))

    }
}

  结果应显示3个单元格,每个单元格都有一个测试标签  该应用程序不会崩溃或显示任何错误,但单元格没有出现

3 个答案:

答案 0 :(得分:0)

func collectionView(_ collectionView: UICollectionView, layout
collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath:
IndexPath) -> CGSize {
    return CGSize(width: view.frame.**height**, height: 50) }

我不确定,但你真的想通过view.frame.height来设置单元格的宽度吗?

答案 1 :(得分:0)

您需要在viewDidLoad()中确认dataSourcedelegate - :

override func viewDidLoad() {
    super.viewDidLoad()
    self.collectionView!.backgroundColor = UIColor(red: 153/255, green: 204/255, blue: 153/255, alpha: 1)

    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = false
    self.collectionView!.alwaysBounceVertical = true
    // Register cell classes
    self.collectionView!.register(threadCell.self, forCellWithReuseIdentifier: reuseIdentifier)

        self.collectionView.delegate = self
        self.collectionView.dataSource = self

    // Do any additional setup after loading the view.
}

答案 2 :(得分:0)

请检查此委托方法

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
   return CGSize(width: view.frame.height, height: 50)
}

您正在创建collectionViewCell的宽度等于视图高度。

如果您想知道如何以编程方式创建集合视图:您可以从以下链接查看项目:

Explain How to create collection in code with auto-layout.