【问题标题】:viewForSupplementaryElementOfKind is not setting the label text until Collection View is scrolledviewForSupplementaryElementOfKind 在 Collection View 滚动之前不会设置标签文本
【发布时间】:2020-04-19 22:40:46
【问题描述】:

我正在使用viewForSupplementaryElementOfKind 在我的收藏视图中生成标题。

标题 (SectionHeader) 是 Storyboard 中的部分标题附件,仅包含 1 个插座。

class SectionHeader: UICollectionReusableView {
    @IBOutlet weak var sectionHeaderlabel: UILabel!
}

这是我对viewForSupplementaryElementOfKind的实现

func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind:
    String, at indexPath: IndexPath) -> UICollectionReusableView {

    print("SECTION TITLE (brand of bindings) --------> \(sectionTitle)")

    if let sectionHeader = allCollectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "bindingsID", for: indexPath) as? SectionHeader{
        sectionHeader.sectionHeaderlabel.text = "Select \(sectionTitle)"
        return sectionHeader
    }
    return UICollectionReusableView()

}

sectionTitle 通过 segue 设置。

问题是当这个 View Controller 加载时,标题显示为“Select”

当我将标题从屏幕上滚动出来,然后又回到屏幕上时,标题会正确显示:"Select Burton Bindings"

我在 viewWillAppear 中测试了sectionTitle,并打印了正确的数据。

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    print("viewWillAppear ----- \(sectionTitle)")
}

(打印viewWillAppear ----- Burton Bindings

我想我的问题是由于 viewForSupplementaryElementOfKind 的生命周期,以及它何时被调用?

如何在 VC 加载时显示部分标题,而不是在屏幕上滚动标题才能显示?

【问题讨论】:

    标签: swift uicollectionview collectionview uicollectionreusableview sectionheader


    【解决方案1】:

    我看到了两种可能的替代方案(如果我首先正确理解了您的用例,则更可取)。

    1) 在视图控制器实例化时(即在加载视图之前)使 sectionTitle 可用

    2) 在出现之前重建部分布局(当您的标题可用时) - 这很重

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        print("viewWillAppear ----- \(sectionTitle)")
    
        // as it is available here force rebuild sections
       self.collectionView.collectionViewLayout.invalidateLayout() 
    }
    

    【讨论】:

      【解决方案2】:

      这里的问题是 viewForSupplementaryElementOfKind 在 vi​​ewWillAppear 之前被调用。在 viewWillAppear 中,我正在重新加载 Collection View。为了正确显示节标题,我所要做的就是在 viewWillAppear 中更新它,.reloadItems

      override func viewWillAppear(_ animated: Bool) {
          super.viewWillAppear(animated)
          // ---
          allCollectionView.reloadItems(at: allCollectionView.indexPathsForVisibleItems)
      
          // Access the SectionHeader and update the title now.
          let headerView = allCollectionView.visibleSupplementaryViews(ofKind: UICollectionView.elementKindSectionHeader)[0] as! SectionHeader
          headerView.sectionHeaderLabel.text = "Select \(sectionTitle)"
      
      }
      

      viewForSupplementaryElementOfKind 中没有设置文本。

      这里的技巧是在 viewWillAppear 中更新 headerView sectionHeaderLabel 文本 collectionView 被重新加载。

      【讨论】:

        猜你喜欢
        • 2011-04-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多