Jay
Jay

Reputation: 185

How do I use multiple UICollectionViewCells in a collectionview within a view controller.

I have two cell classes:

class statusCell: UICollectionViewCell {

var textLabel: UILabel!

required init(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
}

override init(frame: CGRect) {
    super.init(frame:frame)

    let textFrame = CGRect(x: 0, y: 0, width: 150, height: 200)
    textLabel = UILabel(frame: textFrame)
    textLabel.font = UIFont.systemFontOfSize(UIFont.smallSystemFontSize())
    textLabel.textAlignment = .Center
    contentView.addSubview(textLabel)

}

override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
}
}

and

class imagePostCell: UICollectionViewCell {

var textLabel: UILabel!
var imageView: UIImageView!

required init(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
}

override init(frame: CGRect) {
    super.init(frame:frame)

    imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 159, height: 225))
    imageView.contentMode = UIViewContentMode.ScaleAspectFit
    contentView.addSubview(imageView)

    let textFrame = CGRect(x: 0, y: imageView.frame.size.height, width: 159, height: 125)
    textLabel = UILabel(frame: textFrame)
    textLabel.font = UIFont.systemFontOfSize(UIFont.smallSystemFontSize())
    textLabel.textAlignment = .Center
    contentView.addSubview(textLabel)
}

override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
}

}

Here is the view controller:

let statusCellReuseId = "statusCell"
let imagePostCellReuseId = "imagePostCell"
class ProfileViewController: UIViewController, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource {

@IBOutlet var postsView: UICollectionView!


let sectionInsets = UIEdgeInsets(top: 0.0, left: 0.0, bottom: 0.0, right: 0.0)

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
}

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

//num of posts per request / page
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return 10
}

//creation of posts
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    var cell: statusCell!
    var cell2: imagePostCell!
    if((indexPath.row + 2) % 2 == 0) {
        cell = collectionView.dequeueReusableCellWithReuseIdentifier(statusCellReuseId, forIndexPath: indexPath) as statusCell
        if cell == nil {
            cell = statusCell(frame: CGRectMake(0, 0, collectionView.frame.size.width, collectionView.frame.size.height))
        }
        cell.backgroundColor = UIColor.orangeColor()
        cell.textLabel.text = "Hello mate!"
    }
    else {
        cell2 = collectionView.dequeueReusableCellWithReuseIdentifier(imagePostCellReuseId, forIndexPath: indexPath) as imagePostCell
        if cell2 == nil {
            cell2 = imagePostCell(frame: CGRectMake(0, 0, collectionView.frame.size.width, collectionView.frame.size.height))
        }
        cell2.backgroundColor = UIColor.blueColor()
        cell2.textLabel.text = "Goodbyte!"
        cell2.imageView.image = UIImage(named: "buton")
    }
    return cell
}

//num of sections with posts
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
    //#warning Incomplete method implementation -- Return the number of sections
    return 1
}

//height of cell
func collectionView(collectionView: UICollectionView!,layout collectionViewLayout: UICollectionViewLayout!, sizeForItemAtIndexPath indexPath: NSIndexPath!) -> CGSize {
    return CGSize(width: 159, height: 350)
}

//padding from cellphone
func collectionView(collectionView: UICollectionView!,layout collectionViewLayout: UICollectionViewLayout!,insetForSectionAtIndex section: Int) -> UIEdgeInsets {
    return sectionInsets
}


}

I have a collection view within a viewcontroller, the collectionview has two "items" in storyboard with the ids: "statusCell" and "imagePostCell" to try to connect the cells to. The difference between the two cells is one is pure text, while the other is text with an image. Currently, I'm just trying to have the custom cells appear and display sample text / image. But, when I simulate the app, it says the cells in "cellForItemAtIndexPath" are nil. I have tried to create an if-statement to try to remedy the problem, as well as creating the cells programmatically in the viewdidload of the viewcontroller. What is wrong with my code and is there a max to the amount of custom cells that can be used by a single UICollectionView?

Upvotes: 1

Views: 2655

Answers (1)

lojals
lojals

Reputation: 999

You can use the number of prototype cells as you want, but you have to set the cell class in the storyboard, the outlets and the reusable identifier for each cell. And you don't have to check if it's nil. Also if you're not using storyboards you can instantiate it by code without use dequeueReusableCellWithReuseIdentifier .

And remember that if you are using storyboards the method that will be executed is awakeFromNib and not init with frame.

Upvotes: 2

Related Questions