Ich arbeite also mit einem einfachen UICollectionView, 3 Spalten von X Zeilen, wobei X durch die Variable matchesFromSelectedSession.count gesetzt + 1. Hier werden die Delegierten Funktionen sind, die ich für meine Kollektion bin mit:UICollectionView.ReloadData() ändert die Zellenreihenfolge | iOS Swift
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 3
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
let cellHeight = 15.0 as CGFloat
return CGSizeMake(collectionView.bounds.size.width/3, cellHeight)
}
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return matchesFromSelectedSession.count + 1
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let reuseIdentifier = "MatchCollectionViewCell"
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! MatchCollectionViewCell
cell.matchCollectionLabel = UILabel(frame: CGRectMake(0,0,collectionView.bounds.width/3 - 3.0,15))
cell.matchCollectionLabel.textColor = UIColor.blackColor()
cell.systemLayoutSizeFittingSize(cell.frame.size, withHorizontalFittingPriority: UILayoutPriorityDefaultHigh, verticalFittingPriority: UILayoutPriorityDefaultLow)
cell.matchCollectionLabel.font = UIFont(name: "Helvetica Neue", size:12)
if indexPath.section == 0 {
switch indexPath.row {
case 0:
cell.matchCollectionLabel.text = "Match #"
cell.backgroundColor = ImageEditor.colorWithHexString("EFEFF4")
cell.matchCollectionLabel.textAlignment = NSTextAlignment.Right
break
case 1:
cell.matchCollectionLabel.text = "Record"
cell.backgroundColor = ImageEditor.colorWithHexString("EFEFF4")
cell.matchCollectionLabel.textAlignment = NSTextAlignment.Center
break
case 2:
cell.matchCollectionLabel.text = "Outcome"
cell.backgroundColor = ImageEditor.colorWithHexString("EFEFF4")
break
default:
cell.matchCollectionLabel.text = ""
cell.backgroundColor = ImageEditor.colorWithHexString("EFEFF4")
break
}
} else {
switch indexPath.row {
case 0:
cell.matchCollectionLabel.text = "\(indexPath.section)"
if indexPath.section%2 == 0 {
cell.backgroundColor = ImageEditor.colorWithHexString("EFEFF4")
}
cell.matchCollectionLabel.textAlignment = NSTextAlignment.Right
break
case 1:
cell.matchCollectionLabel.text = matchesFromSelectedSession[indexPath.section - 1].matchRecord()
if indexPath.section%2 == 0 {
cell.backgroundColor = ImageEditor.colorWithHexString("EFEFF4")
}
cell.matchCollectionLabel.textAlignment = NSTextAlignment.Center
break
case 2:
let outcome = matchesFromSelectedSession[indexPath.section - 1].matchOutcome()
switch outcome {
case "W":
cell.matchCollectionLabel.text = "Win"
cell.matchCollectionLabel.textColor = UIColor.greenColor()
break
case "D":
cell.matchCollectionLabel.text = "Draw"
cell.matchCollectionLabel.textColor = UIColor.blueColor()
break
case "L":
cell.matchCollectionLabel.text = "Loss"
cell.matchCollectionLabel.textColor = UIColor.redColor()
break
default:
cell.matchCollectionLabel.textColor = UIColor.blackColor()
break
}
if indexPath.section%2 == 0 {
cell.backgroundColor = ImageEditor.colorWithHexString("EFEFF4")
}
break
default:
if indexPath.section%2 == 0 {
cell.backgroundColor = ImageEditor.colorWithHexString("EFEFF4")
}
cell.matchCollectionLabel.text = ""
break
}
}
//add subview if new cell otherwise tag cell to know to reuse next time
if cell.tag != 19 {
cell.addSubview(cell.matchCollectionLabel)
}
cell.tag = 19
return cell
}
Die benutzerdefinierte Zelle, die ich verwende, MatchCollectionViewCell, enthält nur ein Label, MatchCollectionLabel und das ist es. Ich verwende self.matchCollectionView.reloadData() in der viewDidAppear-Funktion der Ansicht, wenn eine sekundäre Ansicht die Tabelle ändert und wieder in diese ursprüngliche Ansicht zurückkehrt.
Ich markiere die Zellen nach der ersten Verwendung, die ich nicht ständig unnötige Untersichten zu der Zelle nach jedem viewDidAppear hinzufügen.
Die Tabelle sieht nach der Ersteinrichtung gut aus, aber nachdem reloadData aufgerufen wurde, wird die Zellenreihenfolge der Auflistungsansicht geändert und durcheinander gebracht. Wenn ich die Anzahl der Zellen bearbeite, wird reloadData() aktualisiert, um die Änderung der Zellennummer korrekt anzuzeigen, aber die Zellen befinden sich immer noch in einer falschen Reihenfolge. Hier
ist ein Bild von vor und nach reload
Jede Hilfe wäre sehr geschätzt! Vielen Dank!
Can Sie posten ein Vorher-Nachher-Bild des collectionView? Das könnte Hinweise geben. – DJohnson
Sie erstellen jedes Mal ein neues Label. Sie fügen es nur als Unteransicht hinzu, wenn das Tag nicht 19 ist, Sie aber immer ein neues Label zuweisen. Dies bedeutet, dass Sie beim erneuten Verwenden einer Zelle eine neue Beschriftung aktualisieren, nicht die, die der Zelle hinzugefügt wurde, sodass Ihre Änderungen nicht sichtbar sind. Die Tag-Überprüfung unten sollte sich am Anfang der Funktion befinden und Sie sollten nur dann ein neues Label erstellen, wenn das Tag nicht 19 ist. Sie könnten auch einfach 'cell.matchCollectionLabel' als optional definieren und es auf – Paulw11
@ prüfen. DJohnson Ich fügte ein Bild des Problems hinzu, aber Paulw11 lieferte die Antwort, die ich brauchte. Danke Leute! –