如何使用故事板获取字符串数组,即[String]以显示在自定义集合视图单元格中

时间:2017-10-06 19:18:11

标签: swift text uicollectionview label cell

在故事板中:

数据源和委托从LFCollectionView连接到LFCollectionViewController。 reuseIdentifier在CollectionViewCell中设置为LFCell。 CollectionViewCell中的标签名为cellLabel,并具有自定义LFCollectionViewCell的插座。我希望outlet cellLabel在集合中每个单元容器中显示字符串数组lFContainerLabel中的元素。我错过了什么,或者说这一切都错了。我没有笔尖。我不知道如何使用它们。在这种情况下,笔尖是必须的吗?

import UIKit

var lFContainerLabel: [String] = []
private let reuseIdentifier = "LFCell"

class LFCollectionView: UICollectionView { }
class LFNavigationController: UINavigationController { }
class LFCollectionViewCell: UICollectionViewCell {
    @IBOutlet weak var cellLabel: UILabel!

}

class LFCollectionViewController: UICollectionViewController,UICollectionViewDelegateFlowLayout {

    override func viewDidLoad() {
        super.viewDidLoad()

        self.collectionView!.register(LFCollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    override func numberOfSections(in LFCollectionViewController: UICollectionView) -> Int {

        return 1
    }

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

        return lFContainerLabel.count
    }

    override func collectionView(_ LFCollectionViewController: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        let lFCell = LFCollectionViewController.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as UICollectionViewCell!

        var myIndex: Int = 0

        lFCell?.backgroundColor = UIColor.black
        lFCell?.accessibilityElements = lFContainerLabel

        myIndex = indexPath.item

        lFCell?.cellLabel?.text = lFContainerLabel[myIndex]
        return lFCell!
    }

}

0 个答案:

没有答案