问题
This is my first time working with a CollectionView . I have a simple button which is repeated 9 times since I am populating it with a string array . I want to change the background color of the first element and I can do that however on Scroll I get other elements with that same background color too and I only want the first element to have that background color . This is my code
class HomeC: UIViewController,CollectionViewDelegate,UICollectionViewDataSource {
@IBOutlet weak var CategoriesView: UICollectionView!
var CategoryIndex = 0
var categoryTypes = ["All","General","Sports","Local Issues","Questions","Politics","Events","Advertise","Sell","Services"]
override func viewDidLoad() {
super.viewDidLoad()
CategoriesView.delegate = self
CategoriesView.dataSource = self
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return categoryTypes.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CategoriesCVC", for: indexPath) as! CategoriesCVC
cell.items.tag = indexPath.row
cell.items.setTitle(categoryTypes[indexPath.row], for: .normal)
if categoryTypes[indexPath.row] == "All" && indexPath.row == CategoryIndex {
cell.items.backgroundColor = UIColor(hexString: BlueBackground)
cell.items.setTitleColor(.white, for: .normal)
}
return cell
}
}
With that code I have my first element which is "All" has the proper background however if I slide across the collectionView I get other elements also with the blue background . How can I fix this issue ? any suggestions would be great .
回答1:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CategoriesCVC", for: indexPath) as! CategoriesCVC
cell.items.tag = indexPath.row
cell.items.setTitle(categoryTypes[indexPath.row], for: .normal)
if categoryTypes[indexPath.row] == "All" && indexPath.row == CategoryIndex {
cell.items.backgroundColor = UIColor(hexString: BlueBackground)
cell.items.setTitleColor(.white, for: .normal)
} else {
// set your normal cell color here
cell.items.backgroundColor = UIColor(hexString: NormalCellColor)
cell.items.setTitleColor(.normalTitleColoe, for: .normal)
}
return cell
}
Since collection view uses the dequeued cell, it takes the old cell background. that is blue color. if you revert the cell color in the else block, that will fix your issues
if you want to do in more Professional way. on cell prepareForReuse, do your cleanups.
override open func prepareForReuse() {
super.prepareForReuse()
// Set your default background color, title color etc
}
回答2:
Since your cell is reused, you need to provide default values. The best way is to override prepareForReuse method under your CategoriesCVC class.
override open func prepareForReuse() {
super.prepareForReuse()
cell.items.backgroundColor = UIColor.black //Any default BG color you are using when not selected
cell.items.setTitleColor(.red, for: .normal) //Any default Title color you are using when not selected
}
回答3:
Here is no need to match both conditions in cellForItemAt , either one of them is sufficient. If your array is fixed in sequence then just write indexPath.row == 0 and if your array is having fixed title values for button then you need to match categoryTypes[indexPath.row] == "All" this single condition. See below code:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CategoriesCVC", for: indexPath) as! CategoriesCVC
cell.items.tag = indexPath.row
cell.items.setTitle(categoryTypes[indexPath.row], for: .normal)
if indexPath.row == 0 {
cell.items.backgroundColor = UIColor(hexString: BlueBackground)
cell.items.setTitleColor(.white, for: .normal)
} else {
cell.items.backgroundColor = UIColor(hexString: DefaultCellColor)
cell.items.setTitleColor(.defaultTitleColor, for: .normal)
}
return cell
}
来源:https://stackoverflow.com/questions/54414600/ios-swift-collectionview-how-can-i-set-the-correct-background-color-on-index