繁体   English   中英

在UICollectionViewCell内设置UILabel

[英]Setting a UILabel inside a UICollectionViewCell

所以事情是我在TableView的CollectionView内有4个UICollectionViewCells。 (我将TableViewController设置为CollectionView的DataSource和Delegate)。

现在,我在CKRecord中存储了一个由4个元素组成的字符串数组。 如何在4个单元格内设置标签,以便它们显示数组的每个字符串?

看起来是这样的:

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

    }

现在显然,这将行不通,因为我基本上将每个单元格的标签设置为数组本身。 如何编写遍历每个CvCell标签的for循环,或者如何指定第三个单元格的标签以将其设置为labelContent [2]的值?

更新:

完全忘记提了,云中的数据结构基本上是这样的:

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

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

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

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

而且,如果我尝试执行@ 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]

但是我想要的是:

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

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

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

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

对不起,我很难解释这一点,但是我希望有人得到我想要做的事情?

首先,您不会返回一个单元格。 您最终将返回4个单元格,因为您规定在numberOfItemsInSection方法的每个节中都需要4个项目。

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

cellForItemAtIndexPath协调您要如何显示每个单元格。 因为您声明一个节中有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

}

设置labelContent数组后,通过将indexPath.row传递到数组中,在每个索引处获取myString。 因此,对于第一个单元格(0索引),它为您提供了数组中的第一个字符串(0索引)。

编辑

创建轮询时,请使用indexPath.section以便在显示数据时保持所有数组的正确顺序。

let poll = polls[collectionView.tag]

编辑2

在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
}

然后,当您使collectionView出队时,可以通过调用collectionView.tag(如上所示)来访问适当的数组,以获取tableViewCell的indexPath。

更好的解决方案,因此您不必将collectionView嵌套在tableView中:

这样可以为您提供所需的部分数量。 “轮询”数组中每个数组的一部分:

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

这将为每个相应部分获取子数组,并允许该collectionView部分中的项目数与该数组中的项目数相对应:

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

在此处显示您的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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM