You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

RxCocoa绑定UICollectionView报Static member rx cannot be using错误如何解决

错误原因

你遇到的编译错误是collectionView.rx.items方法的调用语法错误导致的:你在传入cellIdentifier和cellType两个参数后,没有补全方法参数的右括号)就直接写了单元格配置闭包,导致编译器无法正确识别方法签名,误判为你要调用UICollectionView类型上的静态rx成员,所以抛出了该错误。另外你原代码中dispose(by:)的拼写也错误,正确写法是disposed(by:)。

正确绑定代码

修正后的viewDidLoad部分代码如下:

override func viewDidLoad() {
    super.viewDidLoad()

    presenter?.getProductList()
        .bind(to: collectionView.rx.items(
            cellIdentifier: ProductCollectionViewCell.identifier,
            cellType: ProductCollectionViewCell.self
        )) { index, productEntity, cell in
            // 在此处实现单元格配置逻辑,示例:
            cell.renderData(productEntity)
        }
        .disposed(by: disposeBag)
}

注意事项

  • 确保当前文件头部已经导入import RxSwift和import RxCocoa
  • 确保你已经提前调用collectionView.register(ProductCollectionViewCell.self, forCellWithReuseIdentifier: ProductCollectionViewCell.identifier)完成单元格注册,避免运行时崩溃

内容的提问来源于stack exchange,提问作者Farhandika

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.30 08:39:03