Swift中UICollectionView的代理调用:可将Proxy作为方法参数
在Swift中实现可传递代理对象的UICollectionView代理转发
针对你想要实现的「将UICollectionView的代理对象作为方法参数传递」的需求,我整理了完整的实现方案,核心是通过一个代理转发类来承接UICollectionView的数据源/代理方法,同时允许动态传入目标代理对象进行调用转发,这样就能灵活地将代理逻辑作为参数传递复用。
完整实现代码
// 定义代理转发类,承接UICollectionView的数据源和代理方法 class CollectionProxy: NSObject, UICollectionViewDataSource, UICollectionViewDelegate { // 弱引用持有目标代理对象,避免循环引用 weak var targetDelegate: (UICollectionViewDataSource & UICollectionViewDelegate)? // 关联对应的UICollectionView实例 weak var collectionView: UICollectionView? // 初始化时传入目标代理和关联的UICollectionView init(targetDelegate: (UICollectionViewDataSource & UICollectionViewDelegate)?, collectionView: UICollectionView) { self.targetDelegate = targetDelegate self.collectionView = collectionView super.init() // 将当前代理类设置为collectionView的数据源与代理 collectionView.dataSource = self collectionView.delegate = self } // MARK: - UICollectionViewDataSource 方法转发 func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { // 转发调用到目标代理,默认返回0避免崩溃 return targetDelegate?.collectionView(collectionView, numberOfItemsInSection: section) ?? 0 } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { guard let cell = targetDelegate?.collectionView(collectionView, cellForItemAt: indexPath) else { fatalError("目标代理必须实现cellForItemAt方法") } return cell } // 封装cell复用方法,方便外部调用 func dequeueReusableCell(withReuseIdentifier identifier: String, for indexPath: IndexPath) -> UICollectionViewCell { guard let validCollectionView = collectionView else { fatalError("未关联有效的UICollectionView实例") } return validCollectionView.dequeueReusableCell(withReuseIdentifier: identifier, for: indexPath) } // MARK: - UICollectionViewDelegate 可选方法转发(示例) func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { // 用可选链调用,避免目标代理未实现该方法时崩溃 targetDelegate?.collectionView?(collectionView, didSelectItemAt: indexPath) } } // 使用示例:在ViewController中集成 class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate { private var collectionView: UICollectionView! private var collectionProxy: CollectionProxy! override func viewDidLoad() { super.viewDidLoad() // 初始化UICollectionView let flowLayout = UICollectionViewFlowLayout() flowLayout.itemSize = CGSize(width: 100, height: 100) flowLayout.minimumInteritemSpacing = 10 flowLayout.minimumLineSpacing = 10 collectionView = UICollectionView(frame: view.bounds, collectionViewLayout: flowLayout) collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "CustomCell") collectionView.backgroundColor = .white view.addSubview(collectionView) // 初始化代理转发类,将当前ViewController作为目标代理传入 collectionProxy = CollectionProxy(targetDelegate: self, collectionView: collectionView) } // 实现数据源方法,由代理转发类调用 func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return 15 } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionProxy.dequeueReusableCell(withReuseIdentifier: "CustomCell", for: indexPath) cell.backgroundColor = .random return cell } // 实现代理方法(可选) func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { print("选中了第\(indexPath.section)组第\(indexPath.item)个Cell") } } // 扩展UIColor,方便示例生成随机颜色 extension UIColor { static var random: UIColor { return UIColor( red: CGFloat.random(in: 0...1), green: CGFloat.random(in: 0...1), blue: CGFloat.random(in: 0...1), alpha: 1.0 ) } }
关键设计要点
- 弱引用避免循环:
targetDelegate和collectionView都用weak修饰,防止出现循环引用导致内存泄漏。 - 灵活的代理传递:初始化
CollectionProxy时可以传入任意遵循UICollectionViewDataSource和UICollectionViewDelegate的对象,实现代理逻辑的复用和动态切换。 - 安全的方法转发:对于必须实现的数据源方法,添加默认值或断言;对于可选代理方法,使用可选链调用,保证代码的健壮性。
- 封装复用逻辑:将
dequeueReusableCell方法封装在代理类中,外部可以直接通过代理类调用,简化代码结构。
内容的提问来源于stack exchange,提问作者Richard Topchii
相关产品推荐
相关产品推荐

