ios 学习之UICollectionView 的属性学习

398 阅读2分钟
    // 滑动组件 懒加载
    lazy var collectionView : UICollectionView = {

        // 类似于android LayoutManager 计算view的摆放规则

        let layout = UICollectionViewFlowLayout()

        // 设置item的宽高

        layout.itemSize = CGSize(width: w, height: h)

        layout.minimumLineSpacing = 7 //行间距

        layout.minimumInteritemSpacing = 0 //item之间的间距

        // 设置UICollectionView 的尺寸 全屏 + 左右间距8 + item之间的间距 7

        let collectionView = UICollectionView(

            frame:CGRect(x: 8,y: 8,width:  view.bounds.width - 16,height: view.bounds.height - 8 - CGFloat(bottomSafeAreaHeight)),

            collectionViewLayout:layout

        )


        collectionView.backgroundColor = .clear

        collectionView.delegate = self

        collectionView.dataSource = self

        collectionView.showsVerticalScrollIndicator = **false**

        // CollectionCell 每一个item

        collectionView.register(CollectionCell.**self**, forCellWithReuseIdentifier: reuseID)

        return collectionView

    }()

collectionView.dataSource = self 指定数据源 一般写法

extension RecommendController : UICollectionViewDataSource {

    

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
         // 返回item的个数
        return self.imgs.count

    }

    

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        // 返回每一个item对应的view
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseID, for: indexPath) **as**! CollectionCell

        return cell

    }

    

}

collectionView.delegate = self 设置item的事件处理

# [管理选择Cells]()
// MARK: 是否选中某个item
func collectionView(_ collectionView: UICollectionView, shouldSelectItemAtIndexPath indexPath: NSIndexPath) -> Bool

// MARK: 选中某个item
func collectionView(_ collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath)

// MARK: 是否取消选中某个item
func collectionView(collectionView: UICollectionView, shouldDeselectItemAtIndexPath indexPath: NSIndexPath) -> Bool 

// MARK: 取消选中
func collectionView(collectionView: UICollectionView, didDeselectItemAtIndexPath indexPath: NSIndexPath)

# [2 管理Cell高亮]()
// MARK: 能否选中高亮
func collectionView(collectionView: UICollectionView, shouldHighlightItemAtIndexPath indexPath: NSIndexPath) -> Bool

// MARK: 高亮
func collectionView(collectionView: UICollectionView, didHighlightItemAtIndexPath indexPath: NSIndexPath)

// MARK: 高亮取消
func collectionView(collectionView: UICollectionView, didUnhighlightItemAtIndexPath indexPath: NSIndexPath)

# [增加和移除视图]()
// MARK: cell显示
func collectionView(collectionView: UICollectionView, willDisplayCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath)

// MARK: cell消失
func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath)

// MARK: Header或Footer显示
func collectionView(collectionView: UICollectionView, willDisplaySupplementaryView view: UICollectionReusableView, forElementKind elementKind: String, atIndexPath indexPath: NSIndexPath)

// MARK: Header或Footer消失
func collectionView(collectionView: UICollectionView, didEndDisplayingSupplementaryView view: UICollectionReusableView, forElementOfKind elementKind: String, atIndexPath indexPath: NSIndexPath)

# [管理布局变化]()

// MARK: collectionViewLayout: UICollectionViewLayout发生改变
func collectionView(collectionView: UICollectionView, transitionLayoutForOldLayout fromLayout: UICollectionViewLayout, newLayout toLayout: UICollectionViewLayout) -> UICollectionViewTransitionLayout

// MARK: cell可移动时的起始偏移
func collectionView(collectionView: UICollectionView, targetContentOffsetForProposedContentOffset proposedContentOffset: CGPoint) -> CGPoint {
    if self.yjPrint == YJPrint.HandlingLayoutChanges 

// MARK: 移动cell从originalIndexPath到proposedIndexPath
func collectionView(collectionView: UICollectionView, targetIndexPathForMoveFromItemAtIndexPath originalIndexPath: NSIndexPath, toProposedIndexPath proposedIndexPath: NSIndexPath) -> NSIndexPath

# [管理Cells长点击事件]()
// MARK: 长按,是否将要显示Action菜单(剪切、拷贝、粘贴)
func collectionView(collectionView: UICollectionView, shouldShowMenuForItemAtIndexPath indexPath: NSIndexPath) -> Bool

// MARK: 长按,是否显示Action菜单(剪切、拷贝、粘贴)
func collectionView(collectionView: UICollectionView, canPerformAction action: Selector, forItemAtIndexPath indexPath: NSIndexPath, withSender sender: AnyObject?) -> Bool 

// MARK: 点击菜单中的某个选项
func collectionView(collectionView: UICollectionView, performAction action: Selector, forItemAtIndexPath indexPath: NSIndexPath, withSender sender: AnyObject?) {