繁体   English   中英

CollectionView没有注册单元格

[英]CollectionView not registering cells

我有一个viewController,里面有一个collectionView。 我很确定自己已正确配置了所有内容,但未渲染任何单元格。 我添加了适当的委托和数据源,并仔细检查了一下是否正在加载数据,但是否正在填充单元格

import UIKit
import UIKit
import Alamofire
import AlamofireNetworkActivityIndicator
import SwiftLocation
import CoreLocation
import AMScrollingNavbar

class NewHomeFeedControllerViewController: UIViewController {
    let detailView = EventDetailViewController()
    var allEvents = [Event]()
    let customCellIdentifier1 = "customCellIdentifier1"
    var grideLayout = GridLayout(numberOfColumns: 2)
    let refreshControl = UIRefreshControl()
    var newHomeFeed: NewHomeFeedControllerViewController?
      let paginationHelper = PaginationHelper<Event>(serviceMethod: PostService.showEvent)
    lazy var dropDownLauncer : DropDownLauncher = {
        let launcer = DropDownLauncher()
        launcer.newHomeFeed = self
        return launcer
    }()

    // 1 IGListKit uses IGListCollectionView, which is a subclass of UICollectionView, which patches some functionality and prevents others.
    let collectionView: UICollectionView = {
        // 2 This starts with a zero-sized rect since the view isn’t created yet. It uses the UICollectionViewFlowLayout just as the ClassicFeedViewController did.
        let view = UICollectionView(frame: CGRect.zero, collectionViewLayout: UICollectionViewFlowLayout())
        // 3 The background color is set to white
        view.backgroundColor = UIColor.white
        return view
    }()
    func handleDropDownMenu(){
        dropDownLauncer.showDropDown()
    }
    func configureCollectionView() {
        // add pull to refresh
        refreshControl.addTarget(self, action: #selector(reloadHomeFeed), for: .valueChanged)
        collectionView.addSubview(refreshControl)
    }
    func reloadHomeFeed() {
        self.paginationHelper.reloadData(completion: { [unowned self] (events) in
            self.allEvents = events

            if self.refreshControl.isRefreshing {
                self.refreshControl.endRefreshing()
            }

            DispatchQueue.main.async {
                self.collectionView.reloadData()
            }
        })
    }

    func categoryFetch(dropDown: DropDown){
        navigationItem.title = dropDown.name
        paginationHelper.category = dropDown.name
        configureCollectionView()
        reloadHomeFeed()
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        view.addSubview(collectionView)
        collectionView.contentInset = UIEdgeInsetsMake(15, 0, 0, 0)
        navigationItem.title = "Home"
        collectionView.dataSource = self
        collectionView.delegate = self
        collectionView.collectionViewLayout = grideLayout
        collectionView.reloadData()
        collectionView.register(CustomCell.self, forCellWithReuseIdentifier: customCellIdentifier1)
        //  self.navigationItem.hidesBackButton = true
        let backButton = UIBarButtonItem(image: UIImage(named: "menu"), style: .plain, target: self, action: #selector(handleDropDownMenu))
        self.navigationItem.leftBarButtonItem = backButton
        configureCollectionView()
        reloadHomeFeed()
        // Do any additional setup after loading the view.
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        if let navigationController = self.navigationController as? ScrollingNavigationController {
            navigationController.followScrollView(self.collectionView, delay: 50.0)
        }
    }
    override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)

        if let navigationController = navigationController as? ScrollingNavigationController {
            navigationController.stopFollowingScrollView()
        }
    }

     func scrollViewShouldScrollToTop(_ scrollView: UIScrollView) -> Bool {
        if let navigationController = navigationController as? ScrollingNavigationController {
            navigationController.showNavbar(animated: true)
        }
        return true
    }

    override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
        super.viewWillTransition(to: size, with: coordinator)
        grideLayout.invalidateLayout()
    }
    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
        return true
    }
}


extension NewHomeFeedControllerViewController: UICollectionViewDataSource {
     func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return allEvents.count
    }
    // The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
     func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        let customCell = collectionView.dequeueReusableCell(withReuseIdentifier: customCellIdentifier1, for: indexPath) as! CustomCell
        let imageURL = URL(string: allEvents[indexPath.item].currentEventImage)
        print(imageURL ?? "")
        customCell.sampleImage.af_setImage(withURL: imageURL!)
        return customCell
    }
}


extension NewHomeFeedControllerViewController: UICollectionViewDelegateFlowLayout{
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        if indexPath.item == 0 || indexPath.item == 1 {
            return CGSize(width: view.frame.width, height: grideLayout.itemSize.height)
        }else{
            return grideLayout.itemSize
        }
    }
}

知道会发生什么吗?

对不起,你们忘了添加

  override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        collectionView.frame = view.bounds
    }

哈哈谢谢你的帮助

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM