CollectionView selectItem tintColor不突出显示或选择项目

时间:2017-06-12 20:42:18

标签: swift

我在下面的MenuClass中有这个集合视图:

class MenuBar: UIView, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
    lazy var collectionView: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
        cv.backgroundColor = UIColor.rgb(colorLiteralRed: 230, green: 32, blue: 31, alpha: 1)
        cv.dataSource = self
        cv.delegate = self
        return cv
    }()

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

        addSubview(collectionView)
        collectionView.register(MenuCell.self, forCellWithReuseIdentifier: cellId)
        //NOT WORKING
        let selectIndexPath = IndexPath(item: 0, section: 0)
        collectionView.selectItem(at: selectIndexPath, animated: false, scrollPosition: [])

        addConstraintsWithFormat(format: "H:|[v0]|", views: collectionView)
        addConstraintsWithFormat(format: "V:|[v0]|", views: collectionView)

        backgroundColor = UIColor.rgb(colorLiteralRed: 230, green: 32, blue: 31, alpha: 1)
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! MenuCell
        cell.imageView.image = UIImage(named: imageName[indexPath.row])?.withRenderingMode(UIImageRenderingMode.alwaysTemplate)
        cell.imageView.tintColor = UIColor.rgb(colorLiteralRed: 91, green: 14, blue: 13, alpha: 1)
        return cell
    }
}

这是我在上面的集合视图中注册的MenuCell类:

class MenuCell: BaseCell {

    let imageView: UIImageView = {
        let iv = UIImageView()
        return iv
    }()


    override var isHighlighted: Bool {
        didSet {
            imageView.tintColor = isHighlighted ? UIColor.white : UIColor.rgb(colorLiteralRed: 91, green: 14, blue: 13, alpha: 1)
        }
    }

    override var isSelected: Bool {
        didSet {
            imageView.tintColor = isSelected ? UIColor.white : UIColor.rgb(colorLiteralRed: 91, green: 14, blue: 13, alpha: 1)
        }
    }

    override func setUpViews() {
        super.setUpViews()
        addSubview(imageView)
        addConstraintsWithFormat(format: "H:[v0(28)]", views: imageView)
        addConstraintsWithFormat(format: "V:[v0(28)]", views: imageView)

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

我的问题是MenuBar方法中这些代码行中的init类:

let selectIndexPath = IndexPath(item: 0, section: 0)
collectionView.selectItem(at: selectIndexPath, animated: false, scrollPosition: [])

没有选择集合视图第一项并将其突出显示为默认选定项。我认为它与此行有关:cell.imageView.tintColor = UIColor.rgb(colorLiteralRed: 91, green: 14, blue: 13, alpha: 1) cellForItemAt方法MenuBar内的isSelected以及isHighLightedMenuCell内的重写属性(imageViewMenuCell) 1}}。

我不确定这种着色是如何在Uncaught (in promise) TypeError: Cannot read property 'call' of undefined (我在/* harmony import */ var __WEBPACK_IMPORTED_MODULE_1__base_base_icon__ = __webpack_require__(567); 上定义)或我是否正确使用这些属性的。有人可以让我知道如何使用着色和属性来实现这一目标。主页按钮突出显示为白色:

enter image description here

2 个答案:

答案 0 :(得分:0)

好的,所以我在MenuBar中的cellForItemAt方法中更改了这一行:

cell.imageView.image = UIImage(named: imageName[indexPath.row])?.withRenderingMode(UIImageRenderingMode.alwaysTemplate)

为:

cell.image = UIImage(named: imageName[indexPath.row])?.withRenderingMode(UIImageRenderingMode.alwaysTemplate)

删除imageView并因某种原因修复了问题,现在默认选择第一项。任何人都可以让我知道为什么这有用吗?

答案 1 :(得分:0)

错误是因为您在内的MenuBar类中为 imageView 而不是单元格设置 tintColor collectionView(...,cellForItemAt ...)

//错误的方式

cell.imageView.tintColor = UIColor.rgb(colorLiteralRed: 91, green: 14, blue: 13, alpha: 1)

将tintColor设置为单元格,它将执行

//正确的方式

cell.tintColor = UIColor.rgb(colorLiteralRed: 91, green: 14, blue: 13, alpha: 1)