Ich möchte die Farbe von CircleView und Text in Label in Zelle in CollectionView mit Funktion ändern.Ändern Sie die Farbe und den Text der Elemente in der Zelle in collectionView
Klasse meiner Zelle:
class CustomCell: UICollectionViewCell {
override init(frame: CGRect) {
super.init(frame: frame)
setupViews()
}
var timerLabel:UILabel = {
let label = UILabel()
label.text = "5"
label.frame = CGRect(x: 200, y: 10, width: 60, height: 60)
label.backgroundColor = .white
label.textAlignment = NSTextAlignment.center
return label
}()
var circleView: UIView = {
let circleView = UIView()
circleView.frame = CGRect(x: 100, y: 10, width: 60, height: 60)
circleView.backgroundColor = .red
circleView.layer.cornerRadius = 30
circleView.layer.masksToBounds = true
return circleView
}()
func setupViews() {
backgroundColor = .white
addSubview(timerLabel)
addSubview(circleView)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setupViews()
}
}
erstellen Collection in VC:
func collectionViewSet() {
collectionView?.frame.size.height = (view.frame.height/100) * 70
collectionView?.backgroundColor = .white
collectionView?.register(CustomCell.self, forCellWithReuseIdentifier: cellId)
}
//-------------- (CollectionView Configure) -------------------
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 5
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width:view.frame.width ,height: collectionView.frame.height/6)
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId , for: indexPath)
customCell.tag = indexPath.row + 1
return cell
}
Und in meinem erstellt func zum Beispiel setColor() Ich möchte ranodm Farbe erzeugen (durch UIColor) für Circle und Zufallszahl für TimerLable generieren, aber nur in einer Zelle. Für die nächste Zelle möchte ich eine andere Farbe und Nummer generieren. Gibt es eine Möglichkeit, das zu tun, habe ich versucht, indexPath.row und func didselectedItemForindexPath aber es funktioniert überhaupt nicht. Vielleicht habe ich etwas falsch gemacht.
Teilen Sie die ss Ihrer Ausgabe –
Aber Ausgabe von was? Ich verwende kein Storyboard – Hvosz
Ausgabe auf Simulator, wenn Sie Ihren Code ausführen –