UICollectionView 单元格未正确突出显示
UICollectionView Cells not highlighting properly
我正在尝试添加选择多个集合视图单元格并在选择单元格时更改背景颜色的操作。我还想在取消选择单元格时将背景颜色更改回原始颜色。它正在工作,但是当我滚动集合视图时,背景颜色已更改的单元格消失了。我怀疑我可能需要跟踪 indexPath.row 并将其保存到数组中,但我不确定如何正确实现它。
var indexArray = [Int]()
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ShoeCell", for: indexPath) as! ShoeSizesCell
cell.backgroundColor = UIColor.textFieldBackgroundColorGray
cell.shoeSize.textColor = UIColor.init(hexString: "#737373")
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
// indexArray.append(indexPath.row)
if let currentCell = collectionView.cellForItem(at: indexPath) as? ShoeSizesCell {
if currentCell.backgroundColor == UIColor.textFieldBackgroundColorGray {
currentCell.backgroundColor = .primaryColor
currentCell.shoeSize.textColor = .white
}
else if currentCell.backgroundColor == .primaryColor {
currentCell.backgroundColor = UIColor.textFieldBackgroundColorGray
currentCell.shoeSize.textColor = UIColor.init(hexString: "#737373")
}
}
}
当你在单元格中显示一些数据时,你可以为它制作一个模型。
struct Shoe {
let size: String
var isSelected: Bool
}
在ShoeSizesCell
中添加Shoe
类型变量。并覆盖 UICollectionViewCell
的 isSelected
属性
class ShoeSizesCell: UICollectionViewCell {
var shoe: Shoe? {
didSet {
guard let shoe = shoe else { return }
self.shoeSize?.text = shoe.size
if shoe.isSelected {
self.backgroundColor = .primaryColor
self.shoeSize.textColor = .white
}
else {
self.backgroundColor = UIColor.textFieldBackgroundColorGray
self.shoeSize.textColor = UIColor.init(hexString: "#737373")
}
}
}
override var isSelected: Bool {
didSet {
if self.isSelected {
self.backgroundColor = .primaryColor
self.shoeSize.textColor = .white
}
else {
self.backgroundColor = UIColor.textFieldBackgroundColorGray
self.shoeSize.textColor = UIColor.init(hexString: "#737373")
}
}
}
}
在UIViewController
中,创建一个Shoe
类型的数组来保存单元格的数据。您可以在 viewDidLoad()
方法中分配数据。
var shoes = [Shoe]()
将 UICollectionView
的 allowsSelection
和 allowsMultipleSelection
属性设置为 true
。
collectionView.allowsSelection = true
collectionView.allowsMultipleSelection = true
在 cellForItem
方法中将 Shoe
数据传递给单元格。
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ShoeSizesCell", for: indexPath) as? ShoeSizesCell {
cell.shoe = shoes[indexPath.item]
return cell
}
return UICollectionViewCell()
}
修改 didSelectItem 方法如下。
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
shoes[indexPath.item].isSelected = true
}
并覆盖 didDeselectItemAt
方法。
func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
shoes[indexPath.item].isSelected = false
}
我正在尝试添加选择多个集合视图单元格并在选择单元格时更改背景颜色的操作。我还想在取消选择单元格时将背景颜色更改回原始颜色。它正在工作,但是当我滚动集合视图时,背景颜色已更改的单元格消失了。我怀疑我可能需要跟踪 indexPath.row 并将其保存到数组中,但我不确定如何正确实现它。
var indexArray = [Int]()
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ShoeCell", for: indexPath) as! ShoeSizesCell
cell.backgroundColor = UIColor.textFieldBackgroundColorGray
cell.shoeSize.textColor = UIColor.init(hexString: "#737373")
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
// indexArray.append(indexPath.row)
if let currentCell = collectionView.cellForItem(at: indexPath) as? ShoeSizesCell {
if currentCell.backgroundColor == UIColor.textFieldBackgroundColorGray {
currentCell.backgroundColor = .primaryColor
currentCell.shoeSize.textColor = .white
}
else if currentCell.backgroundColor == .primaryColor {
currentCell.backgroundColor = UIColor.textFieldBackgroundColorGray
currentCell.shoeSize.textColor = UIColor.init(hexString: "#737373")
}
}
}
当你在单元格中显示一些数据时,你可以为它制作一个模型。
struct Shoe {
let size: String
var isSelected: Bool
}
在ShoeSizesCell
中添加Shoe
类型变量。并覆盖 UICollectionViewCell
isSelected
属性
class ShoeSizesCell: UICollectionViewCell {
var shoe: Shoe? {
didSet {
guard let shoe = shoe else { return }
self.shoeSize?.text = shoe.size
if shoe.isSelected {
self.backgroundColor = .primaryColor
self.shoeSize.textColor = .white
}
else {
self.backgroundColor = UIColor.textFieldBackgroundColorGray
self.shoeSize.textColor = UIColor.init(hexString: "#737373")
}
}
}
override var isSelected: Bool {
didSet {
if self.isSelected {
self.backgroundColor = .primaryColor
self.shoeSize.textColor = .white
}
else {
self.backgroundColor = UIColor.textFieldBackgroundColorGray
self.shoeSize.textColor = UIColor.init(hexString: "#737373")
}
}
}
}
在UIViewController
中,创建一个Shoe
类型的数组来保存单元格的数据。您可以在 viewDidLoad()
方法中分配数据。
var shoes = [Shoe]()
将 UICollectionView
的 allowsSelection
和 allowsMultipleSelection
属性设置为 true
。
collectionView.allowsSelection = true
collectionView.allowsMultipleSelection = true
在 cellForItem
方法中将 Shoe
数据传递给单元格。
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ShoeSizesCell", for: indexPath) as? ShoeSizesCell {
cell.shoe = shoes[indexPath.item]
return cell
}
return UICollectionViewCell()
}
修改 didSelectItem 方法如下。
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
shoes[indexPath.item].isSelected = true
}
并覆盖 didDeselectItemAt
方法。
func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
shoes[indexPath.item].isSelected = false
}