在加载时选择集合视图单元格 - 自定义UICollectionView类

时间:2018-03-27 02:37:40

标签: ios swift uicollectionview

我有一个自定义集合视图(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")
}
}

1 个答案:

答案 0 :(得分:0)

您的数据源设置在哪里?你是单独加载它然后做一个reloadData()吗?

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

您还可以添加到viewWillAppear / viewDidAppear方法,因为在填充集合视图后调用此方法