我正在尝试通过 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/