简体   繁体   English

在UICollectionViewCell内设置UILabel

[英]Setting a UILabel inside a UICollectionViewCell

So the thing is I have 4 UICollectionViewCells inside CollectionView that is inside a TableView. 所以事情是我在TableView的CollectionView内有4个UICollectionViewCells。 (I set the TableViewController as the DataSource and Delegate of the CollectionView). (我将TableViewController设置为CollectionView的DataSource和Delegate)。

Now, I've stored a string array of 4 elements inside a CKRecord. 现在,我在CKRecord中存储了一个由4个元素组成的字符串数组。 How can I set the label inside the 4 cells, so that they display each string of the array? 如何在4个单元格内设置标签,以便它们显示数组的每个字符串?

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 eg the third cell to set it to the value of labelContent[2]? 如何编写遍历每个CvCell标签的for循环,或者如何指定第三个单元格的标签以将其设置为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: 而且,如果我尝试执行@ user3353890提出的建议,则会为我的表视图提供以下结果:

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? 对不起,我很难解释这一点,但是我希望有人得到我想要做的事情?

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. 您最终将返回4个单元格,因为您规定在numberOfItemsInSection方法的每个节中都需要4个项目。

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

cellForItemAtIndexPath coordinates how you want to display each cell. cellForItemAtIndexPath协调您要如何显示每个单元格。 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. 因为您声明一个节中有4个项目,所以此方法将被调用4次,每次在该节中被调用为项目时将返回1个单元格。

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

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

    let poll = polls[collectionView.tag] // 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. 设置labelContent数组后,通过将indexPath.row传递到数组中,在每个索引处获取myString。 So for the 1st cell (0 index) it gives you the 1st string in your array (0 index). 因此,对于第一个单元格(0索引),它为您提供了数组中的第一个字符串(0索引)。

Edit 编辑

when you create poll, use indexPath.section in order to keep the correct order of all your arrays while displaying data. 创建轮询时,请使用indexPath.section以便在显示数据时保持所有数组的正确顺序。

let poll = polls[collectionView.tag]

Edit 2 编辑2

In your tableView cellForRowAtIndexPath Method, when you create a cell, set the cell's collectionView.tag to indexPath.section 在tableView cellForRowAtIndexPath方法中,创建单元格时,将单元格的collectionView.tag设置为indexPath.section。

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("customCell", forIndexPath: indexPath) as! CustomCell
    cell.collectionView.delegate = self
    cell.collectionView.dataSource = self
    cell.collectionView.tag = indexPath.section
    return cell
}

Then when you dequeue your collectionView, you can access the proper array by calling collectionView.tag -as seen above- in order to get the indexPath of the tableViewCell. 然后,当您使collectionView出队时,可以通过调用collectionView.tag(如上所示)来访问适当的数组,以获取tableViewCell的indexPath。

A better solution so you don't have to nest a collectionView within a tableView: 更好的解决方案,因此您不必将collectionView嵌套在tableView中:

This gives you the number of sections you want. 这样可以为您提供所需的部分数量。 One section for each array in your "polls" array: “轮询”数组中每个数组的一部分:

func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
    return polls.count
}

This will take the sub-array for each corresponding section and allow the number of items in that collectionView section to correspond with the number of items in the array: 这将为每个相应部分获取子数组,并允许该collectionView部分中的项目数与该数组中的项目数相对应:

func collectionView(collectionView: UICollectionView,
                numberOfItemsInSection section: Int) -> Int {
    let poll = polls[section] as? [String]
    return poll.count
}

Display your collectionView cells here: 在此处显示您的collectionView单元格:

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]

    cell.cellLabel.titleLabel?.text = myString

    return cell

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 UICollectionViewCell 中的 UILabel 为 nil - UILabel inside UICollectionViewCell is nil 滚动后设置 UICollectionViewCell 中的 UILabel? - UILabel In UICollectionViewCell is setting after scrolling? UICollectionViewCell 内的 UILabel 未正确更新 - UILabel inside of UICollectionViewCell is not updating correctly 如何在考虑性能的情况下旋转UICollectionViewCell内的UILabel? - How to rotate a UILabel inside a UICollectionViewCell considering performance? 在Custom UICollectionViewCell中更改UILabel的文本颜色 - Change text color of UILabel inside Custom UICollectionViewCell 在自定义UICollectionViewCell内调整UILabel的大小并重新放置 - Resizing and re-positioning UILabel inside custom UICollectionViewCell 快速,以编程方式更改UICollectionViewCell和UILabel(在单元格内)的宽度 - Swift, Change width of UICollectionViewCell and UILabel(inside the cell) programmatically 如何在UICollectionViewCell内更改UILabel的值? - How do I change the value of a UILabel inside of a UICollectionViewCell? 设置UICollectionViewCell内的UITableView集的Delegate和数据源 - setting Delegate and datasource of a UITableView set inside a UICollectionViewCell 将UIView和UILabel添加到UICollectionViewCell - Add UIView and UILabel to UICollectionViewCell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM