collectionView快速加载XIB而不是数组中的内容

时间:2018-12-02 15:01:50

标签: ios swift uicollectionview

我有一个收集视图,该视图应显示从firebase加载的对象中的项目数组。但是,当应用程序首次加载时,它只会显示没有加载内容的xib(有时内容已加载,而其他时候则没有)。只有当我向数组添加新项并重新加载集合视图时,内容才会加载到视图中。这是应用程序首次打开时的外观。集合视图仅显示了xib collection view not loaded,但是,在添加新项目之后,集合视图看起来应为collection view as it should look,我的问题是,在显示之前,我如何确保始终加载集合视图?卸载的XIB?谢谢。

方法:

ggplot() +
  geom_boxplot(
    data = fixed, aes(x = Test, y = Percentage, fill = Test)
  ) +
  geom_rect( # HERE IS UR BOX
    data = data.frame(),
    aes(xmin = -Inf, xmax = Inf, ymin = 0.565, ymax = Inf),
    size = 2.5, fill = "#00000000", color = "black"
  ) +
  stat_summary(
    data = fixed, aes(Test, Percentage),
    fun.data = function(y) {
      data.frame(
        y = 0.6,
        label = paste(round(mean(y), 2))
      )
    },
    geom = "text", size = 3
  ) +
  scale_y_continuous(
    limits = c(0, 0.6), breaks = seq(0, 0.6, 0.1),
    labels = c("0%", "10%", "20%", "30%", "40%", "50%", "Mean")
  ) +
  scale_fill_manual(
    values = c(
      "#00441b", "#006d2c", "#238b45", "#41ab5d", 
      "#74c476", "#a1d99b", "#c7e9c0", "#e5f5e0"
    )
  ) +
  coord_cartesian(clip = "off") + # WE NEED TO TURN CLIPPING OFF
  labs(
    x = "Test", y = "Percentage", title = "Title"
  ) +
  theme_bw() +
  theme(
    legend.position = "none", # YOU HAD THIS IN A SEPARATE theme(). IT PAYS TO HAVE A CODE STYLE
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    panel.background = element_rect(fill = "grey90", colour = NA),
    plot.background = element_rect(fill = "grey90", colour = NA),
    plot.title = element_text(size = 12, hjust = 0.5),
    plot.margin = unit(c(1, 1, 1, 1), "cm"),
    axis.text.y = element_text( # MAKE THE "Mean" BIG & BOLD 
      size = c(rep(10, 6), 20), face = c(rep("plain", 6), "bold")
    )
  )

var currentUser: User! var listCategories: [String] = ["Friends Lists", "Friends", "People"] var lists = [Media]() func numberOfSections(in collectionView: UICollectionView) -> Int { return listCategories.count } func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { if section == 0 { return lists.count }else{ return 0 } } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: Storyboard.listCell, for: indexPath) as! ListCell cell.currentUser = currentUser cell.media = lists[indexPath.item] cell.layer.applySketchShadow(color: UIColor.black, alpha: 0.08, x: 0, y: 0, blur: 10, spread: 0) cell.layer.cornerRadius = 20 cell.layer.masksToBounds = false return cell } //section header view func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView { let sectionHeaderView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: Storyboard.sectionHeader, for: indexPath) as! SectionHeaderView let category = listCategories[indexPath.section] sectionHeaderView.sectionTitle = category return sectionHeaderView }

中调用的下一个方法
viewWillAppear

内部媒体类

func fetchMedia() {
    Media.observeNewMedia { (media) in
        if !self.lists.contains(media) {
            self.lists.insert(media, at: 0)
            self.collectionView.reloadData()
        }
    }
}

在collectionViewCell内部

class func observeNewMedia(_ completion: @escaping (Media) -> Void)
{

    WADatabaseReference.media.reference().observe(.childAdded) { (snapshot) in
        let media = Media(dictionary: snapshot.value as! [String : Any])
        completion(media)
    }

}

导入基金会 导入SAMCache

import UIKit

}

1 个答案:

答案 0 :(得分:0)

至少在我看来,现在好像您不使用空数据重新加载collectionView,而是仅加载.xib文件中的内容。 为避免这种情况,请尝试在collectionView.reloadData()中的viewDidLoad中或在数组仍为空的情况下使用此方法。