在提取CollectionView的delegate和DataSource时遇到的问题

本文探讨了UICollectionView中代理方法未能正常工作的问题。通过对比不同实例化的委托对象,揭示了内存管理和对象持有关系对于代理模式实现的重要性。
在提取CollectionView的delegate和DataSource时发现以下代码是有问题的
CollectionViewDelegate *delegate= [[CollectionViewDelegate alloc]init......];
    collection.delegate = delegate;

delegate的提取类里不会执行代理方法,原因猜测是未被当前对象持有,引用计数问题(只是猜测)

下面这么写是没问题的

self.collectionViewDelegate= [[CollectionViewDelegate alloc]init];
    collection.delegate = self.collectionViewDelegate;
自己记录一下
什么意思 给我完整修改 // // DeviceListView.swift // SurveillanceHome // // Created by MaCong on 2025/12/3. // Copyright © 2025 tplink. All rights reserved. // import UIKit class DeviceListView: UIView { private let collectionView: UICollectionView private var dataSource: [TPSSDeviceForDeviceList] = [] // MARK: - 初始化 override init(frame: CGRect) { let layout = UICollectionViewFlowLayout() layout.scrollDirection = .vertical collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout) super.init(frame: frame) setupView() } required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } private func setupView() { // 配置 Collection View collectionView.backgroundColor = .clear collectionView.showsVerticalScrollIndicator = false collectionView.delegate = self collectionView.dataSource = self // 注册 Cell collectionView.register(DeviceCollectionViewCell.self, forCellWithReuseIdentifier: "DeviceCollectionViewCell") addSubview(collectionView) collectionView.snp.makeConstraints { make in make.edges.equalToSuperview() } } // MARK: - 公共方法:用于刷新数据 func reloadData(with devices: [TPSSDeviceForDeviceList]) { self.dataSource = devices collectionView.reloadData() } func updateConstraintsIfNeeded() { collectionView.layoutIfNeeded() } } // MARK: - UICollectionViewDataSource extension DeviceListView: UICollectionViewDataSource { func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return dataSource.count } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "DeviceCollectionViewCell", for: indexPath) as! DeviceCollectionViewCell let device = dataSource[indexPath.item] cell.configure(with: device) return cell } } // MARK: - UICollectionViewDelegate extension DeviceListView: UICollectionViewDelegate { func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { let device = dataSource[indexPath.item] print("选中设备: \(device.alias)") // 可以通过闭包或代理通知外部 onDeviceSelected?(device) } } // MARK: - 代理/回调支持(可选) extension DeviceListView { var onDeviceSelected: ((TPSSDeviceForDeviceList) -> Void)? { didSet { collectionView.delegate = nil collectionView.delegate = self } } }
12-09
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值