专业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 注册自定义的cell HMNewFeatureCell
    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 是哪一页
    • collectionViewcellForItemAtIndexPath 代理方法里面设置 cellimageIndex

      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 设置cellimageIndex 属性,后 cell 根据不同的 imageIndex 显示不同的图片
      // 不同页面显示不同图片
      var imageIndex: Int? = 0 {
        // 当collectionView 来设置imageIndex的时候,根据imageIndex去加载图片
        didSet {
            backgroundImageView.image = UIImage(named: "new_feature_\(imageIndex! + 1)")
        }
      }
      

results matching ""

    No results matching ""