Ios 为多个UICollectionView单元格中的标签指定不同的文本
我有一个自定义单元格的UICollectionView。一切正常,但我不知道如何为每个单元格中的标签指定不同的文本。我有一个部分,每个部分有三个项目。我希望所有三个项目都有完全相同的布局。我的想法是,我需要为每个单元格分配不同的标识符,但我不确定如何为多个单元格标识符设置不同的文本 非常感谢您的帮助,谢谢Ios 为多个UICollectionView单元格中的标签指定不同的文本,ios,swift,uicollectionview,Ios,Swift,Uicollectionview,我有一个自定义单元格的UICollectionView。一切正常,但我不知道如何为每个单元格中的标签指定不同的文本。我有一个部分,每个部分有三个项目。我希望所有三个项目都有完全相同的布局。我的想法是,我需要为每个单元格分配不同的标识符,但我不确定如何为多个单元格标识符设置不同的文本 非常感谢您的帮助,谢谢 let reuseIdentifier = "directionCell" override func numberOfSectionsInCollectionView(collect
let reuseIdentifier = "directionCell"
override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 1
}
override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 3
}
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! CustomCollectionViewCell
cell.titleLabel.text = "Current title"
cell.subtitleLabel.text = "Choose to start"
cell.layer.cornerRadius = 10
return cell
当您将numberOfItemsInSection设置为3时,cellForItemAtIndexPath也将被调用3次。因此,如果您想要显示一组数据或信息,那么在cellForItemAtIndexPath函数中使用它来获得所需的结果。因此,假设您有一个标题数组,请在中使用该数组 cellForItemAtIndexPath
var titles: [String] = ["Title1", "Title2", "Title3"]
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
{
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! CustomCollectionViewCell
cell.titleLabel.text = self.titles[indexPath.row]
cell.subtitleLabel.text = "Choose to start"
cell.layer.cornerRadius = 10
return cell
}
如果您有3个单元,并且3个单元有不同的内容,只需创建3个CollectionViewCel并连接IB插座,然后像这样调用主题:-
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
var cell: UICollectionViewCell!
let reuseIdentifier = "CustomCollectionViewCell"
let cell1 = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! CustomCollectionViewCell
if indexPath.row == 0 {
cell1.titleLabel.text = "Current title"
cell1.subtitleLabel.text = "Choose to start"
cell1.layer.cornerRadius = 10
cell = cell1
} else if indexPath.row == 1 {
cell2.titleLabel.text = "Current title"
cell2.subtitleLabel.text = "Choose to start"
cell2.layer.cornerRadius = 10
cell = cell2
} else {
cell3.titleLabel.text = "Current title"
cell3.subtitleLabel.text = "Choose to start"
cell3.layer.cornerRadius = 10
cell = cell3
}
return cell
}
向我们展示你的源代码。我用源代码编辑。这只是我所提到的标准样板代码,因为我不确定如何处理将文本分配给多个单元格的问题。只需使用某种可以获取索引(如数组)的方法设置文本,使用
indexPath
作为选择正确文本的方法。这是最基本的东西——如果你是Cocoa Touch和iOS新手,我建议你学习iTunesU教程。谢谢你的回答,Christian。非常感谢。我认为对于集合视图,您可以使用索引路径的项和节(而不是行和节)。