gpt4 book ai didi

ios - 在加载时选择 Collection View 单元格 - 自定义 UICollectionView 类

转载 作者:行者123 更新时间:2023-11-30 11:38:16 25 4
gpt4 key购买 nike

我有一个自定义 Collection View (tabBarCollectionView),它应该在选择第一个单元格时加载,如下面的代码所示。然而,这是行不通的。我知道这一点,因为选择单元格时应该是不同的颜色,但事实并非如此。

override init(frame: CGRect) {
super.init(frame: frame)

tabBarCollectionView.register(TabBarCell.self, forCellWithReuseIdentifier: cellIdentifier)

addSubview(tabBarCollectionView)
tabBarCollectionView.translatesAutoresizingMaskIntoConstraints = false
addConstraintsWithFormat("H:|[v0]|", views: tabBarCollectionView)
addConstraintsWithFormat("V:|[v0]|", views: tabBarCollectionView)

tabBarCollectionView.selectItem(at: IndexPath(item: 0, section: 0), animated: false, scrollPosition: [])
}

自定义 UIView 类的完整代码插入如下:

import Foundation
import UIKit

class TabBar: UIView, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {

let cellIdentifier = "Cell"
let imageNames = ["homeIcon", "exploreIcon", "addIcon", "inboxIcon", "profileIcon"]

lazy var tabBarCollectionView: UICollectionView = {

// All collection view implementations in here

let layout = UICollectionViewFlowLayout()
let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)

collectionView.dataSource = self
collectionView.delegate = self

collectionView.backgroundColor = UIColor.clear

return collectionView
}()

override init(frame: CGRect) {
super.init(frame: frame)

tabBarCollectionView.register(TabBarCell.self, forCellWithReuseIdentifier: cellIdentifier)

addSubview(tabBarCollectionView)
tabBarCollectionView.translatesAutoresizingMaskIntoConstraints = false
addConstraintsWithFormat("H:|[v0]|", views: tabBarCollectionView)
addConstraintsWithFormat("V:|[v0]|", views: tabBarCollectionView)

self.tabBarCollectionView.allowsSelection = true
self.tabBarCollectionView.selectItem(at: IndexPath(item: 0, section: 0), animated: false, scrollPosition: [])
}

override func layoutSubviews() {
super.layoutSubviews()

tabBarCollectionView.allowsSelection = true
tabBarCollectionView.selectItem(at: IndexPath(item: 0, section: 0), animated: false, scrollPosition: [])
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 5
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellIdentifier, for: indexPath) as! TabBarCell

cell.backgroundColor = UIColor.clear
cell.tabBarImageView.image = UIImage(named: imageNames[indexPath.item])?.withRenderingMode(.alwaysTemplate)
cell.tabBarImageView.tintColor = UIColor.pinpointGrey

return cell
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

return CGSize(width: frame.width / 5, height: frame.height)
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {

return 0
}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}



}

class TabBarCell: UICollectionViewCell {

let tabBarImageView: UIImageView = {

let imageView = UIImageView()
imageView.image = UIImage(named: "homeIcon")?.withRenderingMode(.alwaysTemplate)
imageView.tintColor = UIColor.pinpointGrey
return imageView
}()

override init(frame: CGRect) {
super.init(frame: frame)

setupViews()
}

func setupViews() {

backgroundColor = UIColor.clear

addSubview(tabBarImageView)
addConstraintsWithFormat("H:[v0(42)]", views: tabBarImageView)
addConstraintsWithFormat("V:[v0(42)]", views: tabBarImageView)

addConstraint(NSLayoutConstraint(item: tabBarImageView, attribute: .centerX, relatedBy: .equal, toItem: self, attribute: .centerX, multiplier: 1, constant: 0))
addConstraint(NSLayoutConstraint(item: tabBarImageView, attribute: .centerY, relatedBy: .equal, toItem: self, attribute: .centerY, multiplier: 1, constant: 0))
}

override var isHighlighted: Bool {

didSet {
if tabBarImageView.isHighlighted == true {
self.tabBarImageView.tintColor = UIColor.pinpointBlue
} else {
self.tabBarImageView.tintColor = UIColor.pinpointGrey
}
}
}

override var isSelected: Bool {

didSet {
tabBarImageView.tintColor = isSelected ? UIColor.pinpointBlue : UIColor.pinpointGrey
}
}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}

最佳答案

你的数据源集在哪里?您是否单独加载它,然后执行 reloadData()?

如果是,那么您应该选择该单元格,而不是在 viewDidLoad 方法期间选择,因为在加载期间, Collection View 可能尚未填充。

您还可以添加到 viewWillAppear/viewDidAppear 方法,因为在填充 Collection View 后会调用该方法

关于ios - 在加载时选择 Collection View 单元格 - 自定义 UICollectionView 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49503545/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com