配置集合查看单元格

时间:2015-12-11 18:15:41

标签: ios swift swift2

您好我有一个内置CollectionView的ViewController。我无法配置集合视图的自定义视图单元格。

这是我的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!
}

当我运行它们时,我得到的只是一个视图单元,除了它的背景颜色。再次请求帮助,这是我的决赛。感谢

2 个答案:

答案 0 :(得分:0)

我可以看到可能存在问题的两件事。

首先,可能这就是问题,类名必须用CamelCase编写,从大写开始。因此,请确保将C:\Users\YourUser\.m2\repository\org\codehaus\groovy\ 的所有引用更改为achievmentViewCell(包括文件名)。所以它看起来像这样:

AchievmentViewCell

其次,请确保在故事板上添加了let cell:AchievmentViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("achievmentCell", forIndexPath: indexPath) as! AchievmentViewCell 小区标识符。

enter image description here

答案 1 :(得分:0)

为您找到错误。因此,在调用collectionView时,在代码中调用泛型collectionView而不是您在achievmentViewController中链接的collectionView。

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