如何解决在Swift 4中单击单元格时,如何在CollectionView外部更改textLabel?
当我单击单元格时,如何帮助我更改在CollectionView外部但在同一ViewController中的标签中的文本?我需要标签中具有不同单元格的不同文本。
P.S。我是新手:)
解决方法
在单元格的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"
}
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。