ios 快速设置UiCollectionView中单元格的选定颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39293072/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Set selected color of cell in UiCollectionView in swift
提问by Krutarth Patel
I am using collectionview to display data.
我正在使用 collectionview 来显示数据。
each cell's selected and unselected color is there
每个单元格的选定和未选定颜色都在那里
but i want to set default color of first cell in collectionview.
但我想在 collectionview 中设置第一个单元格的默认颜色。
if there are 10 data. my first cell should be like in screenshot
如果有10个数据。我的第一个单元格应该像截图一样
how to achieve this? when i set color at indexpath this issue occurs
如何实现这一目标?当我在 indexpath 设置颜色时会出现此问题
回答by pedrouan
For two different colors based on selected state of the cell, you may need to subclassyour UICollectionViewCell like this:
对于基于单元格选定状态的两种不同颜色,您可能需要像这样子类化UICollectionViewCell:
import UIKit
class YourCollectionViewCell: UICollectionViewCell {
@IBOutlet weak var imageView: UIImageView! // for example - some UI element inside cell ...
override var isSelected: Bool {
didSet {
self.contentView.backgroundColor = isSelected ? UIColor.blue : UIColor.yellow
self.imageView.alpha = isSelected ? 0.75 : 1.0
}
}
}
// and below you will have your original code
class YourViewController: UICollectionViewController {
... etc
Answering your question - for exceptional style of the first cell:
回答您的问题 -第一个单元格的特殊风格:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell ...
if(indexPath.row == 0) { //for first cell in the collection
cell.backgroundColor = UIColor.orange
} else {
cell.backgroundColor = UIColor.green
}
回答by logan.Nguyen
this is my code for this issue. Hope it save time for you :)
这是我针对此问题的代码。希望它为您节省时间:)
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "customCell", for: indexPath) as! CustomCell
if let indexPaths = collectionView.indexPathsForSelectedItems, let indexP = indexPaths.first {
cell.bgView.backgroundColor = indexP == indexPath ? .white : .lightGray
}
return cell }
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if let cell = collectionView.cellForItem(at: indexPath) as? CustomCell {
cell.bgView.backgroundColor = .white }
}
func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
guard let cell = collectionView.cellForItem(at: indexPath) as? CustomCell else {
return
}
cell.bgView.backgroundColor = .lightGray
}
回答by praths
call selectItemAtIndexPath:animated:scrollPosition: method after reloadData method of UICollectionView
在 UICollectionView 的 reloadData 方法之后调用 selectItemAtIndexPath:animated:scrollPosition: 方法
e.g.
例如
let collectionView = UICollectionView()
collectionView.reloadData()
collectionView.selectItemAtIndexPath(NSIndexPath(forItem: 0, inSection: 0), animated: true, scrollPosition: .None)
回答by Santosh
Per to @pedroun's answer, here is my updated answer to make your first cell's default color. Hope this helps you.
根据@pedroun 的回答,这是我更新的答案,用于设置您的第一个单元格的默认颜色。希望这对你有帮助。
@IBOutlet weak var cellImageview: UIImageView!
var cellIndex: Int? {
didSet {
guard let index = cellIndex else { return }
if index != 0 {
contentView.backgroundColor = UIColor.blueColor()//change this to your unselected cell color.
}else{
contentView.backgroundColor = UIColor.redColor()//this is default color for your first cell.
cellImageview.alpha = 1.0
}
}
}
override var selected: Bool {
didSet {
contentView.backgroundColor = selected ? UIColor.redColor(): UIColor.blueColor()//change colors as per your requirements
cellImageview.alpha = selected ? 0.75 : 1.0
}
}
For the cellIndex
property, you need to set it in your cellForRowAtIndexPath
as cell.cellIndex = indexPath.row
对于该cellIndex
属性,您需要将其设置cellForRowAtIndexPath
为cell.cellIndex = indexPath.row
回答by AppHero2
There is two delegate method in UICollectionViewDelegate.
UICollectionViewDelegate 中有两个委托方法。
didDeselectItemAtIndexPath & didSelectItemAtIndexPath
didDeselectItemAtIndexPath 和 didSelectItemAtIndexPath
when you click collection cell, you can get indexPath of the cell. And you can get collection cell by using cellForItemAtIndexPath
当您单击集合单元格时,您可以获取单元格的 indexPath。您可以使用 cellForItemAtIndexPath 获取集合单元格
I have experienced in UITableView and I think UICollectionView is same with following logic.
我在 UITableView 有经验,我认为 UICollectionView 与以下逻辑相同。
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if indexPath.row < arrData.count && arrData.count > 0{
if let selectedCell = tableView.cellForRowAtIndexPath(indexPath) as? GARatesCell {
selectedCell.viewBG.backgroundColor = COLOR_NAV_BAR
selectedCell.lblTitle.textColor = UIColor.whiteColor()
btnShare.backgroundColor = COLOR_NAV_BAR
self.selectedIndex = indexPath.row
}
}
}
func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
if indexPath.row < arrData.count && arrData.count > 0{
let deselectedCell = tableView.cellForRowAtIndexPath(indexPath) as? GARatesCell
if deselectedCell != nil {
deselectedCell?.viewBG.backgroundColor = COLOR_BG_BAR
deselectedCell?.lblTitle.textColor = COLOR_NAV_BAR
}
}
}
回答by C??ng Nguy?n
I know this is not a perfect solution. But it's works.
我知道这不是一个完美的解决方案。但它的作品。
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
collectionView.visibleCells.forEach {
##代码##.backgroundColor = .white
}
}