Customised delegate 自定义代理

来源:互联网 发布:nginx 压力测试 编辑:程序博客网 时间:2024/05/16 13:53

The example is for nesting a smallCollectionVIew into a BigCollectionViewCell, whose collection view is constructed in BigCollectionViewController. Going tomanipulate bigCollectionView in one VC through clicking cell of smallCollectionView in another class.


1. Delegate definition

    Where: BigCollectionViewCell.swift (which file you want to call a delegate function)

    protocol BigCategoryCellDelegate :class {

        func didSelectSmallCell (#indexPath:NSIndexPath)

    }


2. Create delegate property

    Where:BigCollectionViewCell.swift (a property of BigCollectionViewCell class)

    weakvar delegate: BigCategoryCellDelegate?


3. Function call

    Where:BigCollectionViewCell.swift (collectionView = smallCollectionVIew)

    func collectionView(collectionView:UICollectionView, didSelectItemAtIndexPath indexPath:NSIndexPath) {

        self.delegate?.didSelectSmallCell(indexPath: indexPath)

    }

-----------------------------------------s-e-p-e-r-a-t-o-r----------------------------------------------

4. Inherit the delegate

    where: BigCollectionViewController.swift (whose property you want to manipulate)

    class BigCollectionViewController: UIViewController, ...BigCategoryCellDelegate {}


5. Link the delegate

    where:BigCollectionViewController.swift (right after the BigCollectionViewCell() is dequeued)

    var cell = collectionView.dequeueReusableCellWithReuseIdentifier("bigCollectionViewCell", forIndexPath: indexPath) as? BigCollectionViewCell

    cell?.delegate =self


6. Function Definition

    where:BigCollectionViewController.swift

    func didSelectSmallCell (#indexPath:NSIndexPath){

        ...

        bigCollectionView?.reloadData()

    }


0 0
原创粉丝点击