ios - CollectionView 单元格未加载到 View Controller 中

标签 ios swift uicollectionview uitabbarcontroller

我正在尝试通过 Firebase 根据用户数据在 Feed 中加载一些数据,但是,它不起作用。我的应用程序当前经过组织,以便用户进入 CustomTabBarController 并验证登录,并且已创建配置文件,如果需要可以检索它。然后,我通过以下方式将用户发送到提要:

// Go to home feed
        let navController = self.viewControllers![0] as? UINavigationController
        let feedVC = navController?.topViewController as? FeedViewController
        if feedVC != nil {
            feedVC!.getProfilePhotos()
        }

我的第一个问题 - 这是在 CustomTabBarController 上加载 FeedViewController 的正确方法吗?我还打电话提前获取配置文件数据。

getProfilePhotos 是一组委托(delegate)和协议(protocol),并按以下方式返回(我已验证它可以正确检索 photoURL)。然后调试器认为在此之后没有更多的方法可以触发。

func feedProfilePhotosRetrieved(photoURLs: [String]) {

    // Set photos array and reload the tableview
    self.photoURLs = photoURLs
    cardCollectionView.reloadData()
}

这是我的 FeedViewController 类,它的属性和 viewDidLoad()/viewDidAppear()

var feedModel = FeedViewModel()
var associates = UserProfile.shared().associates
var photoURLs = [String]()

@IBOutlet weak var cardCollectionView: UICollectionView!


override func viewDidLoad() {
    super.viewDidLoad()
    associates = UserProfile.shared().associates
    feedModel.delegate = self
    cardCollectionView.delegate = self
    cardCollectionView.dataSource = self
    cardCollectionView.register(CardCollectionViewCell.self, forCellWithReuseIdentifier: "sectionCell")
    cardCollectionView.reloadData()
}

override func viewWillAppear(_ animated: Bool) {
    getProfilePhotos()
}

这是我在 Collection View 中创建单元格的地方。我在“cell”的声明处放置了一个断点,但它没有触发。

extension FeedViewController: UICollectionViewDelegate, UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return associates.count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "sectionCell", for: indexPath) as! CardCollectionViewCell
    let card = UserProfile.shared().associates[indexPath.row]
    cell.name.text = card.name
    cell.poscomp.text = card.title + ", " + card.company

    // Photo that we're trying to display
    let p = photoURLs[indexPath.row]

    // Display photo
    cell.downloadPhoto(p)
    cell.layer.transform = animateCell(cellFrame: cell.frame)
    return cell
} }

是否有任何我遗漏的明显可见的错误?我是否也必须在 reloadingData() 时调用上述函数?感谢您的帮助,如果您需要更多信息,请告诉我。

最佳答案

numberOfItemsInSection 方法应该返回 photoURLs.count

extension FeedViewController: UICollectionViewDelegate, UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return photoURLs.count
}

关于ios - CollectionView 单元格未加载到 View Controller 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56550336/

相关文章:

iphone - 数组值递减

ios - RxSwift 观察数组变化

ios - 向 WebView 添加约束时出错 - Swift

swift - 我怎样才能得到用户邮政编码

ios - 将图像异步加载到 UICollectionView?

ios - 原型(prototype) Collection View 单元格 "Unable to simultaneously satisfy constraints."警告

ios - 添加和删​​除作为单个对象包的联系人(如在 Facebook Messenger 中)

ios - 更改/动画 UITextView 中的最大行数?

ios - 从子 CollectionViewCell 访问父 tableViewCell

javascript - 为 iphone 移动网络定制的警报框