专业java、php、iOS、C++、网页设计、平面设计、网络营销、游戏开发、前端与移动开发培训机构
新特性界面自定义Cell
在
HMNewFeatureViewController.swift
最下面定义HMNewFeatureCell
继承UICollectionViewCell
class HMNewFeatureCell: UICollectionViewCell { // 构造函数 override init(frame: CGRect) { super.init(frame: frame) } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } }
懒加载背景图片控件
backgroundImageView
// MARK: - 懒加载控件 // 背景图片 private lazy var backgroundImageView: UIImageView = UIImageView()
准备UI
, 在构造方法
里面添加为子控件// 构造函数 override init(frame: CGRect) { super.init(frame: frame) // 准备UI setupUI() }
// MARK: - 准备UI private func setupUI() { // 添加为子控件 contentView.addSubview(backgroundImageView) backgroundImageView.backgroundColor = UIColor.brown // 添加约束 backgroundImageView.translatesAutoresizingMaskIntoConstraints = false // 背景 // leading self.contentView.addConstraint(NSLayoutConstraint(item: backgroundImageView, attribute: NSLayoutAttribute.leading, relatedBy: NSLayoutRelation.equal, toItem: self.contentView, attribute: NSLayoutAttribute.leading, multiplier: 1, constant: 0)) // top self.contentView.addConstraint(NSLayoutConstraint(item: backgroundImageView, attribute: NSLayoutAttribute.top, relatedBy: NSLayoutRelation.equal, toItem: self.contentView, attribute: NSLayoutAttribute.top, multiplier: 1, constant: 0)) // trailing self.contentView.addConstraint(NSLayoutConstraint(item: backgroundImageView, attribute: NSLayoutAttribute.trailing, relatedBy: NSLayoutRelation.equal, toItem: self.contentView, attribute: NSLayoutAttribute.trailing, multiplier: 1, constant: 0)) // bottom self.contentView.addConstraint(NSLayoutConstraint(item: backgroundImageView, attribute: NSLayoutAttribute.bottom, relatedBy: NSLayoutRelation.equal, toItem: self.contentView, attribute: NSLayoutAttribute.bottom, multiplier: 1, constant: 0)) }
- 在
viewDidLoad
注册自定义的cellHMNewFeatureCell
self.collectionView!.registerClass(HMNewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)
实现
collectionView
数据源和代理方法override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return ItemCount }
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) return cell }
需要在不同cell里面显示不同的新特性图片
- 在
HMNewFeatureCell
里面定义imageIndex
属性,用来表示当前cell
是哪一页 在
collectionView
的cellForItemAtIndexPath
代理方法里面设置cell
的imageIndex
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) // 设置当前cell是第几个cell cell.imageIndex = indexPath.item return cell }
- 当
collectionView
设置cell
的imageIndex
属性,后cell
根据不同的imageIndex
显示不同的图片// 不同页面显示不同图片 var imageIndex: Int? = 0 { // 当collectionView 来设置imageIndex的时候,根据imageIndex去加载图片 didSet { backgroundImageView.image = UIImage(named: "new_feature_\(imageIndex! + 1)") } }
- 在