Ios 配置集合视图单元格

Ios 配置集合视图单元格,ios,swift,swift2,Ios,Swift,Swift2,嗨,我有一个ViewController,里面有一个CollectionView。我很难配置集合视图的自定义视图单元格 这是我的ViewController import UIKit class achievmentViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate , UICollectionViewDelegateFlowLayout{ @IBOutlet wea

嗨,我有一个ViewController,里面有一个CollectionView。我很难配置集合视图的自定义视图单元格

这是我的ViewController

import UIKit
class achievmentViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate , UICollectionViewDelegateFlowLayout{
    @IBOutlet weak var progressBar: UIProgressView!
    @IBOutlet weak var aCollectionView: UICollectionView!     
    override func viewDidLoad() {        
        super.viewDidLoad()
        aCollectionView.delegate = self
        aCollectionView.dataSource = self
        aCollectionView.backgroundColor = UIColor.whiteColor()
        aCollectionView.registerClass(achievmentViewCell.self, forCellWithReuseIdentifier: "achievmentCell")
        let w = self.view.frame.width-40
        let h = self.view.frame.height-72
        let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
        layout.sectionInset = UIEdgeInsets(top: h*(5/100), left: w*(3.1/100), bottom: h*(5/100), right:  w*(3.1/100))
         layout.itemSize = CGSize(width: w*(27/100), height: h*(30/100))
        aCollectionView = UICollectionView(frame: CGRect(x: 20, y: 72, width: w, height: h), collectionViewLayout: layout)
        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return 1
    }
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 3
    }
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell:achievmentViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("achievmentCell", forIndexPath: indexPath) as! achievmentViewCell

        cell.backgroundColor = UIColor.greenColor()
        cell.setTextForLabel("test")
        cell.Label?.text = "test"
        print(cell.Label?.text)

       // cell.setImageForImageView(UIImage(named: "badge")!)
        cell.imageView?.image = UIImage(named: "badge")!
        return cell
    }



    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }

}
下面是我的collectionViewCell类:

import UIKit
class achievmentViewCell: UICollectionViewCell{
    @IBOutlet weak var Label: UILabel!
    @IBOutlet weak var imageView: UIImageView!
}

当我运行它们时,我得到的只是一个视图单元格,它只有背景色。请再次帮助我,这是我的期末考试。谢谢

我可以看出这里可能有两个问题

首先,很可能这就是问题所在,类名必须用CamelCase写,以大写字母开始。因此,请确保将
AchipmentViewCell
的所有引用更改为
AchipmentViewCell
(包括文件名)。所以它看起来是这样的:

let cell:AchievmentViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("achievmentCell", forIndexPath: indexPath) as! AchievmentViewCell
第二,确保在情节提要上添加了
AcquipmentCell
单元标识符


为您找到了错误。因此,在您的代码中,当您调用collectionView时,您调用的是一个通用collectionView,而不是您在AcquisitionViewController中链接的collectionView

    let cell:achievmentViewCell = acollectionView.dequeueReusableCellWithReuseIdentifier("achievmentCell", forIndexPath: indexPath) as! achievmentViewCell

事实上,故事板中存在一些问题,它允许集合视图具有不同的控制器类:CollectionView和UICollectionView。我选错了。无论如何谢谢你