【问题标题】:CollectionView Cells not loading in View ControllerCollectionView 单元格未在视图控制器中加载
【发布时间】:2019-06-11 19:09:43
【问题描述】:

我正在尝试通过 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 是一组委托和协议,并返回以下方式(我已验证它正确检索 photoURLs)。然后调试器认为在此之后没有更多方法可以触发。

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()
}

这是我在集合视图中创建单元格的地方。我在“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() 时我是否也必须调用上述函数?感谢您的帮助,如果您需要更多信息,请告诉我。

【问题讨论】:

    标签: ios swift uicollectionview uitabbarcontroller


    【解决方案1】:

    方法 numberOfItemsInSection 应该返回 photoURLs.count。

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

    【讨论】:

      猜你喜欢
      • 2020-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多