未在View Controller中加载CollectionView单元格

时间:2019-06-11 19:09:43

标签: 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是一组委托和协议,并以以下方式返回(我已验证它可以正确检索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()
}

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

1 个答案:

答案 0 :(得分:0)

numberOfItemsInSection方法应返回photoURLs.count

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