ios - UIView 上的 CollectionView 单元格实例(委托外部)无法快速运行 5
问题描述
我有一个视图HorizontalMenuCollectionView
,我正在加载集合视图。我可以通过将它与任何视图挂钩来使用它(来自身份检查员)。一切都运行良好。但是现在我想在开始加载此视图时设置第一个项目单元格的背景颜色。但是单元格背景颜色没有改变。我在这里想念什么?提前非常感谢。
这是我试图设置项目单元格的背景颜色的功能
func selectinitialCell() {
let selectedIndexPath = IndexPath(item: 0, section: 0)
let cell = menuCollectionView.dequeueReusableCell(withReuseIdentifier: "HorizontalMenuCollectionViewCell", for: selectedIndexPath) as! HorizontalMenuCollectionViewCell
cell.backgroundColor = UIColor.blue.withAlphaComponent(0.05)
menuCollectionView.reloadData()
}
这是完整的HorizontalMenuCollectionView
:
import UIKit
protocol HorizontalMenuCollectionViewDelegate {
func didSelectItemAtIndexPath(title: String)
}
class HorizontalMenuCollectionView: UIView {
var horizontalMenuCollectionViewDelegate : HorizontalMenuCollectionViewDelegate!
@IBOutlet weak var menuCollectionView: UICollectionView!
var objectArray = [String?]()
var isFirstTimeGettingCalled = true
//This initializer will call from code
override init(frame: CGRect) {
super.init(frame: frame)
self.initialization()
}
//This initializer will call from XIB
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.initialization()
}
func initialization() {
let view = Bundle.main.loadNibNamed("HorizontalMenuCollectionView", owner: self, options: nil)![0] as? UIView
view?.frame = self.bounds
self.autoresizingMask = [.flexibleHeight, .flexibleWidth]
self.addSubview(view!)
registerNib()
selectinitialCell()
}
func selectinitialCell() {
let selectedIndexPath = IndexPath(item: 0, section: 0)
let cell = menuCollectionView.dequeueReusableCell(withReuseIdentifier: "HorizontalMenuCollectionViewCell", for: selectedIndexPath) as! HorizontalMenuCollectionViewCell
cell.backgroundColor = UIColor.blue.withAlphaComponent(0.05)
menuCollectionView.reloadData()
}
func registerNib() {
let horizontalMenuCollectionViewCellNib = UINib(nibName: "HorizontalMenuCollectionViewCell", bundle: nil)
menuCollectionView.register(horizontalMenuCollectionViewCellNib, forCellWithReuseIdentifier: "HorizontalMenuCollectionViewCell")
}
}
extension HorizontalMenuCollectionView: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return objectArray.count
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
return UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 0
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 0
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let text = NSAttributedString(string: objectArray[indexPath.row]!)
return CGSize(width: text.size().width + 80, height: self.bounds.height)
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "HorizontalMenuCollectionViewCell", for: indexPath) as! HorizontalMenuCollectionViewCell
cell.titleLabel.text = objectArray[indexPath.row]!
let backgroundView = UIView()
backgroundView.backgroundColor = UIColor.blue.withAlphaComponent(0.05)
cell.selectedBackgroundView = backgroundView
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
menuCollectionView.scrollToItem(at: indexPath, at: .centeredHorizontally, animated: true)
horizontalMenuCollectionViewDelegate.didSelectItemAtIndexPath(title: objectArray[indexPath.row]!)
}
}
Ans 我正在访问它的视图控制器
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var horizontalMenuCollectionView: HorizontalMenuCollectionView!
override func viewDidLoad() {
super.viewDidLoad()
horizontalMenuCollectionView.horizontalMenuCollectionViewDelegate = self
horizontalMenuCollectionView.objectArray = ["A", "AA", "AAA", "AAAA", "AAAAA"]
}
}
extension ViewController: HorizontalMenuCollectionViewDelegate {
func didSelectItemAtIndexPath(title: String) {
print("\(title)")
}
}
完整的示例项目链接在这里
解决方案
用下面的替换你的 func selectinitialCell() 。
func selectinitialCell() {
menuCollectionView.performBatchUpdates({
self.menuCollectionView.reloadData()
}) { (finish) in
if finish{
let selectedIndexPath = IndexPath(row: 0, section: 0)
self.menuCollectionView.selectItem(at: selectedIndexPath, animated: false, scrollPosition: .left)
}
}
}
推荐阅读
- kubernetes - 拒绝连接到 Kubernetes 服务
- excel - VBA 在范围内使用 right()
- google-app-engine - App Engine 在现有项目上部署错误 500,而不是在新项目上
- javascript - 使用 .env 文件进行单元测试
- video - 如何裁剪视频底部然后删除黑色边框?
- mysql - Mysql将同一张表加入多列(反规范化?)
- sharepoint - Sharepoint 计算列从年、周和日数中获取日期
- java - 将字符串输入存储为数组 Java
- mysql - 在 MySQL 5.7 中使用 `Max()` 和 `GROUP BY`
- c# - 如何读取cmd文件