从字符串数组填充集合视图单元格标签的最简单方法是什么? [英] What's the easiest way of populating a collection view cell's label from an array of strings?

查看:27
本文介绍了从字符串数组填充集合视图单元格标签的最简单方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个字符串数组,如:["Michael", "James", "Rob"],我想用这些名称填充我的 3 个单元格的文本标题.

Let's say I have an string array like: ["Michael", "James", "Rob"], and I want to populate my 3 cell's text title with these names.

我正在使用 UICollectionViewController.

I'm using a UICollectionViewController.

推荐答案

您需要为某个对象(可以是 UICollectionViewController 本身)实现 UICollectionViewDataSource 协议和将其设置为视图的 dataSource 属性.

You need to implement the UICollectionViewDataSource protocol for some object (which can be the UICollectionViewController itself) and set it as your view's dataSource property.

类似于:

class MyCollectionViewController: UICollectionViewController, UICollectionViewDataSource {

    myDataArray: [String] = ["data", "goes", "here"]

    override init(collectionViewLayout layout: UICollectionViewLayout) {
        super.init(layout)
        self.collectionView?.dataSource = self
    }

    collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) {
        return myDataArray.count
    }

    collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) {
        let item = myDataArray[indexPath.row]
        //use "item" to build your cell
        return myCell
    }

}

这篇关于从字符串数组填充集合视图单元格标签的最简单方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆