2017-05-26 3 views
0

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.

+0

Teilen Sie die ss Ihrer Ausgabe –

+0

Aber Ausgabe von was? Ich verwende kein Storyboard – Hvosz

+0

Ausgabe auf Simulator, wenn Sie Ihren Code ausführen –

Antwort

0

Es gibt viele, viele Beispiele zum Erzeugen zufälliger Farben und Zahlen - eine schnelle Suche und Sie haben eine große Auswahl.

Sobald Sie ein paar ausgewählt haben, ändern Sie Ihre cellForItemAt Funktion:

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId , for: indexPath) 
     customCell.tag = indexPath.row + 1 

     let randColor = myRandomColorFunc() 
     let randNumber = myRandomNumberFunc() 

     customCell.circleView.backgroundColor = randColor 
     customCell.timerLabel.text = "\(randNumber)" 

     return cell 
} 
+0

Ok, es ist toll, aber es ist Arbeit "on load". Ich möchte nur Farbe und Nummer auswählen, wenn ich diese Funktion anrufe (zum Beispiel über die Schaltfläche). Es ist möglich? – Hvosz

+0

Nicht sicher, was du meinst ... Willst du die "circleView backgroundColor" der Zelle und den "timerLabel Text" auf zufällige Farbe/Zahl ändern, wenn du auf die Zelle klickst? Oder Sie haben eine Schaltfläche irgendwo und möchten das CollectionView mit neuen zufälligen Farben und Zahlen neu laden? Versuchen Sie * klar * zu erklären, was Sie tun möchten ... – DonMag

+0

Diese zweite Situation, ich habe eine Schaltfläche – Hvosz

1

Ok Ich habe eine Antwort gefunden. Ich poste meinen Code, wenn jemand ähnliches Problem haben wird :)

Verwandte Themen