首页 > 解决方案 > 带有来自数组的文本标签的 UICollectionView

问题描述

我试图有一个显示一系列客户推荐的集合视图。我在内容视图上有另一个集合视图,这就是为什么下面的部分代码包含一个图像集合视图。该应用程序在我的手机上运行良好,但标签未显示在屏幕上。我究竟做错了什么?

@IBOutlet weak var collectionViewTwo: UICollectionView!
let testimonialArray = ["You rock", "You suck"]

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        if collectionView.tag == 1 {
            let cell = collectionViewOne.dequeueReusableCell(withReuseIdentifier: "HomeOneCollectionViewCell", for: indexPath) as? HomeOneCollectionViewCell
            cell!.imageOne.image = imageArrayOne[indexPath.row]
            return cell!
        } else {
            let cell = collectionViewTwo.dequeueReusableCell(withReuseIdentifier: "HomeTwoCollectionViewCell", for: indexPath) as? HomeTwoCollectionViewCell
            cell?.testimonialLabel?.text = testimonialArray[indexPath.row]
            cell?.testimonialLabel?.textColor = UIColor.white
            return cell!
        }
    }

标签: swiftxcodeuicollectionviewlabel

解决方案


猜猜你错误地将文本颜色设置为白色?白色标签不会显示在白色背景上。评论该行应该向您显示结果。粉煤灰。

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    if collectionView.tag == 1 {
        let cell = collectionViewOne.dequeueReusableCell(withReuseIdentifier: "HomeOneCollectionViewCell", for: indexPath) as? HomeOneCollectionViewCell
         cell!.imageOne.image = imageArrayOne[indexPath.row]
        return cell!
    } else {
        let cell = collectionViewTwo.dequeueReusableCell(withReuseIdentifier: "HomeTwoCollectionViewCell", for: indexPath) as? HomeTwoCollectionViewCell
        cell?.testimonialLabel?.text = testimonialArray[indexPath.row]
        // cell?.testimonialLabel?.textColor = UIColor.white
        return cell!
    }
}

请看这里


推荐阅读