我正在使用collectionview来显示数据。
每个单元格的选定和未选择颜色都在那里
但我想在collectionview中设置第一个单元格的默认颜色。
如果有10个数据。我的第一个单元格应该像截图
如何实现这一目标? 当我在indexpath设置颜色时会出现此问题
答案 0 :(得分:30)
对于基于单元格选定状态的两种不同颜色,您可能需要子类化您的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
回答你的问题 - 第一个单元格的特殊风格:
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
}
答案 1 :(得分:3)
这是我的问题代码。希望能为您节省时间:)
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
}
答案 2 :(得分:2)
调用selectItemAtIndexPath:animated:scrollPosition:UICollectionView的reloadData方法之后的方法
e.g。
let collectionView = UICollectionView()
collectionView.reloadData()
collectionView.selectItemAtIndexPath(NSIndexPath(forItem: 0, inSection: 0), animated: true, scrollPosition: .None)
答案 3 :(得分:1)
根据@ pedroun的回答,这是我更新的答案,以制作您的第一个单元格的默认颜色。希望这会对你有所帮助。
$(document).ready(function() {
$(document).on('click', '#new_topic', function() {
console.log("SONO DENTRO");
$('ul[class="formatting-group"]').append('<li tabindex="-1" data-format="schedula" title="Schedula Topic"><i class="fa fa-calendar" aria-hidden="true"></i></li>');
});
});
对于@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
}
}
媒体资源,您需要在cellIndex
中将其设置为cellForRowAtIndexPath
答案 4 :(得分:0)
UICollectionViewDelegate中有两个委托方法。
didDeselectItemAtIndexPath&amp; didSelectItemAtIndexPath
单击集合单元格时,可以获取单元格的indexPath。您可以使用cellForItemAtIndexPath
获取集合单元格我在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
}
}
}
答案 5 :(得分:0)
我知道这不是一个完美的解决方案。但这是可行的。
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
collectionView.visibleCells.forEach {
$0.backgroundColor = .white
}
}