[英]How to change textLabel outside CollectionView when cell is clicked in Swift 4?
你能帮助我如何在单击单元格时更改位于 CollectionView 之外但在同一个 ViewController 中的标签中的文本吗? 我需要不同单元格的标签中的不同文本。
PS 我是新来的 swift :)
在单元格的 didSelectAt 函数中,您可以根据该 indexPath 检索另一个单元格,然后您可以更新该单元格中的标签:
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let otherCell = collectionView.cellForItem(at: IndexPath(item: otherCellsIndexItem, section: otherCellsIndexSection)) as! YourCollectionViewCell
otherCell.label.text = "New Text"
}
}
编辑 1:标签在集合视图之外,然后:
class YourView {
...
@IBOutlet weak var yourLabel: UILabel!
// or
var yourLabel: UILabel?
...
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
self.label.text = "New Text"
// or, if it's UILabel?, then:
self.label?.text = "New Text"
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.