user3545063 user3545063 - 6 months ago 17
Swift Question

Setting a UILabel inside a UICollectionViewCell

So the thing is I have 4 UICollectionViewCells inside CollectionView that is inside a TableView. (I set the TableViewController as the DataSource and Delegate of the CollectionView).

Now, I've stored a string array of 4 elements inside a CKRecord. How can I set the label inside the 4 cells, so that they display each string of the array?

Here's what it looks like:

func collectionView(collectionView: UICollectionView,
numberOfItemsInSection section: Int) -> Int {

return 4
}


func collectionView(collectionView: UICollectionView,
cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! CvCell

let poll = polls[indexPath.row] // polls is a [CKRecord]()
let labelContent = poll["4strings"] as? [String]


cell.cellLabel.titleLabel?.text = labelContent

return cell

}


Now obviously, that's not going to work since I basically set the label of each cell to the array itself. How can I write a for loop that goes through each CvCell label, or rather how can I specify the label of e.g. the third cell to set it to the value of labelContent[2]?

UPDATE:

Totally forgot to mention, the data structure in the cloud basically looks like this:

array1 = [1a, 1b, 1c, 1d]

array2 = [2a, 3b, 2c, 2d]

array3 = [3a, 2b, 3c, 3d]

array4 = [4a, 4b, 4c, 4d]

And if I try to do what @user3353890 proposed, it's giving me the following results for my tableview:

tableview1cell - collectionview1 : [1a, 2b, 3c, 4d] -> these are the collectionviewcell labels

tableview2cell - collectionview2 : [1a, 2b, 3c, 4d]

tableview3cell - collectionview3 : [1a, 2b, 3c, 4d]

tableview4cell - collectionview4 : [1a, 2b, 3c, 4d]

However what I want is:

tableview1cell - collectionview1 : [1a, 1b, 1c, 1d]

tableview2cell - collectionview2 : [2a, 2b, 2c, 2d]

tableview3cell - collectionview3 : [3a, 3b, 3c, 3d]

tableview4cell - collectionview4 : [4a, 4b, 4c, 4d]

I'm sorry I have a really hard time explaining this, but I hope someone gets what I'm trying to do?

Answer

First, you're not returning one cell. You're going to ultimately return 4 cells because you stipulated that you want 4 items in each section in the numberOfItemsInSection method.

func collectionView(collectionView: UICollectionView,
                numberOfItemsInSection section: Int) -> Int {
    return 4
}

cellForItemAtIndexPath coordinates how you want to display each cell. Because you stated that there are 4 items in a section, this method will get called 4 times, returning 1 cell each time it is called to be an item in that section.

func collectionView(collectionView: UICollectionView,
                    cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! CvCell

    let poll = polls[indexPath.section] // polls is a [CKRecord]()
    let labelContent = poll["4strings"] as? [String]

    // This should give you the string that you want.
    let myString = labelContent[indexPath.row]

    // Display the string in the label.
    cell.cellLabel.titleLabel?.text = myString

    return cell

}

After you set the labelContent array, get myString at each index by passing indexPath.row into the array. So for the 1st cell (0 index) it gives you the 1st string in your array (0 index).

Edit

when you create poll, use indexPath.section in order to keep the correct order of all your arrays while displaying data.

let poll = polls[indexPath.section]
Comments