Arrays 如何使单元格索引path.row==数组中的项索引?

Arrays 如何使单元格索引path.row==数组中的项索引?,arrays,swift,uicollectionview,Arrays,Swift,Uicollectionview,我有一个名为cellNames的字符串类型的100项数组。我有一个集合视图,由1个部分组成,其中有100行customCollectionViewCell行。我在找这样的东西- for cellName in cellNames { if cellName.index == cellNames[indexpath.row] { let cellName = cell.nameTextField } } 总之,我需要索引0…100==cellForRowAt 0…100的

我有一个名为
cellNames
的字符串类型的100项数组。我有一个集合视图,由1个部分组成,其中有100行
customCollectionViewCell
行。我在找这样的东西-

for cellName in cellNames {
   if cellName.index == cellNames[indexpath.row] {
     let cellName = cell.nameTextField
    }
}

总之,我需要索引0…100==cellForRowAt 0…100的
cellName
,看起来您正在创建100个静态单元格,并试图用
cellName
填充它们。正确的方法是遵循
UICollectionViewDataSource
并将项数设置为
cellNames
的计数,并使用
cellForItemAt
中提供的indexPath访问数组的每个元素

class ViewController: UIViewController {

    let cellNames = ["1", "2", "3"] // ....

    override func viewDidLoad() {
        super.viewDidLoad()

    }
}

extension ViewController: UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return cellNames.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "customCellId", for: indexPath) as! CustomCell
        let cellName = cellNames[indexPath.item]
        cell.nameTextField.text = cellName
        return cell
    }
}

看起来您正在创建100个静态单元格,并尝试使用
单元格名来填充它们。正确的方法是遵循
UICollectionViewDataSource
并将项数设置为
cellNames
的计数,并使用
cellForItemAt
中提供的indexPath访问数组的每个元素

class ViewController: UIViewController {

    let cellNames = ["1", "2", "3"] // ....

    override func viewDidLoad() {
        super.viewDidLoad()

    }
}

extension ViewController: UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return cellNames.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "customCellId", for: indexPath) as! CustomCell
        let cellName = cellNames[indexPath.item]
        cell.nameTextField.text = cellName
        return cell
    }
}

是否要使用
手机名
填充collectionView?不清楚您在问什么我的收藏视图有100个单元格:CustomCollectionViewCell。customcollectionviewcell有一个名为nameTextField的文本字段。我需要从数组中获取100个项目中的每一个,并使用数组中的一个cellName填充每个单元格。但我需要按顺序排列它们,以便cellName[0]填充cell@[indexPath.row[0]]您想要什么?您想将每个单元格与indexpath组合,还是想知道哪个单元格是第三个单元格?例如,您想用
单元格名填充collectionView
?不清楚您在问什么我的收藏视图有100个单元格:CustomCollectionViewCell。customcollectionviewcell有一个名为nameTextField的文本字段。我需要从数组中获取100个项目中的每一个,并使用数组中的一个cellName填充每个单元格。但我需要按顺序排列它们,以便cellName[0]填充cell@[indexPath.row[0]]您想要什么?您是想将每个单元格与indexpath组合,还是想知道哪一个单元格是第三个单元格