我如何修复为UICollectionView显示错误项目(只显示第一项)的didSelectItemAt函数的错误?

2022-01-28 06:45:21 标签 iosswiftxcodeuicollectionview

我正在尝试建立一个应用程序,有一个视图,显示在收集视图中的足球联赛列表。我已经设置了Firestore数据库来存储所需的数据,并使用以下代码将它们检索到我的应用程序中。

enum LeagueListType {
    case leagueList(viewModels: [LeagueCellViewModel])
}
class LeaguesViewController: UIViewController {
    private var db = Firestore.firestore()
    private var leagues = [LeaguesInfo]()
    private var sections = [LeagueListType]()
    private var collectionView: UICollectionView = UICollectionView(
        frame: .zero,
        collectionViewLayout: UICollectionViewCompositionalLayout { sectionIndex, _ -> NSCollectionLayoutSection? in
            return LeaguesViewController.createSectionLayout()
        })
    override func viewDidLoad() {
        super.viewDidLoad()
        title = "Leagues"
        view.backgroundColor = .systemBackground
        configureCollectionView()
        fetchData()
    }
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        collectionView.frame = view.bounds
    }
    private func configureCollectionView() {
        view.addSubview(collectionView)
        collectionView.register(UICollectionViewCell.self,
                                forCellWithReuseIdentifier: "cell")
        collectionView.register(LeagueListCollectionViewCell.self,
                                forCellWithReuseIdentifier: LeagueListCollectionViewCell.identifier)
        collectionView.dataSource = self
        collectionView.delegate = self
        collectionView.backgroundColor = .systemBackground
    }
    private func configureModels(leagues: [LeaguesInfo]) {
        self.leagues = leagues
        // Configure Models
        sections.append(.leagueList(viewModels: leagues.compactMap({
            return LeagueCellViewModel(id: $0.id,
                                       name: $0.name,
                                       logo: URL(string: $0.logo))
        })))
        collectionView.reloadData()
    }
    private func fetchData() {
        db.collection("leagues").addSnapshotListener { (snapshot, error) in
            DispatchQueue.main.async {
                guard let documents = snapshot?.documents else {
                    print("No league data")
                    return
                }
                self.leagues = documents.compactMap { (documentSnapshot) -> LeaguesInfo in
                    let data = documentSnapshot.data()
                    let id = data["id"] as? String ?? ""
                    let name = data["name"] as? String ?? ""
                    let logo = data["logo"] as? String ?? ""
                    let leaguess = LeaguesInfo(id: id, name: name, logo: logo)
                    self.configureModels(leagues: [leaguess])
                    self.collectionView.reloadData()
                    return leaguess
                }
            }
        }
    }
}

这里是cellForItemAt部分。

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let type = sections[indexPath.section]
        switch type {
        case .leagueList(let viewModels):
            guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: LeagueListCollectionViewCell.identifier,
                                                                for: indexPath) as? LeagueListCollectionViewCell else {
                return UICollectionViewCell()
            }
            let viewModel = viewModels[indexPath.row]
            cell.configure(with: viewModel)
            return cell
        }
    }

我使用以下代码的didSelectItemAt函数在扩展。

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        collectionView.deselectItem(at: indexPath, animated: true)
        let league = leagues[indexPath.row]
        let vc = LeagueViewController(league: league)
        vc.title = league.name
        vc.navigationItem.largeTitleDisplayMode = .never
        navigationController?.pushViewController(vc, animated: true)
    }

问题是didSelectItemAt总是返回错误的项目。它只显示联盟的第一个项目的viewcontroller,不管我选择哪个项目。

你能教教我怎么修理吗?

阅读全文

▼ 版权说明

相关文章也很精彩
推荐内容
更多标签
相关热门
全站排行
随便看看

错说 cuoshuo.com —— 程序员的报错记录

部分内容根据CC版权协议转载;网站内容仅供参考,生产环境使用务必查阅官方文档

辽ICP备19011660号-5

×

扫码关注公众号:职场神器
发送: 1
获取永久解锁本站全部文章的验证码