【问题标题】:Display Section Header UICollectionReusableView显示部分标题 UICollectionReusableView
【发布时间】:2017-06-22 09:04:42
【问题描述】:

我正在开发 iOS 应用程序,但在使用 UICollectionView 单元格时遇到了一些问题。

这次想问一下UICollectionView(UICollectionReusableView)的section header如何显示

我已经实现了如下功能:

public func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {

        switch kind {

        case UICollectionElementKindSectionHeader:

            let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "cellHeader", for: indexPath as IndexPath)
            var labelHeader = headerView.viewWithTag(2) as! UILabel

            if indexPath.section == 0 {
                labelHeader.text = "Specialist Clinic"

            }
            else {
                labelHeader.text = "Medical Support"
            }

            headerView.backgroundColor = UIColor.blue;
            return headerView

        default:
            assert(false, "Unexpected element kind")
        }
    }

但是,它总是给出一个空白的结果。请看下面的屏幕截图

【问题讨论】:

  • 您是否尝试过实现可重用的视图高度委托方法?

标签: ios swift xcode uicollectionview uicollectionreusableview


【解决方案1】:

您需要返回标题的大小。

func collectionView(_ collectionView: UICollectionView,
                     layout collectionViewLayout: UICollectionViewLayout,
                     referenceSizeForHeaderInSection section: Int) -> CGSize {
    return CGSize(width: collectionView.frame.size.width, height: 123) // you can change sizing here 
}

委托方法

  func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
            var reusableview: UICollectionReusableView? = nil
            if kind == UICollectionElementKindSectionHeader {
                reusableview = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "cellHeader", for: indexPath) // cellHeader is your identifier
            var labelHeader = reusableview.viewWithTag(2) as! UILabel

         if indexPath.section == 0 {
                labelHeader.text = "Specialist Clinic"

            }
            else {
                labelHeader.text = "Medical Support"
            }

            headerView.backgroundColor = UIColor.blue;
               
            }
            return reusableview!
        }

【讨论】:

    【解决方案2】:

    我已经为你创建了演示。下载并重新使用到您的代码中。干杯!

    下载链接:https://www.dropbox.com/sh/vzf2tpe0ccf41tv/AABjdPAoaP2sE7YRtUgersq4a?dl=0

    【讨论】:

      【解决方案3】:

      需要在headerView上添加UILabel

            public func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
      
          switch kind {
      
          case UICollectionElementKindSectionHeader:
      
              let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "cellHeader", for: indexPath as IndexPath)
              var labelHeader = headerView.viewWithTag(2) as! UILabel
      
              if indexPath.section == 0 {
                  labelHeader.text = "Specialist Clinic"
      
              }
              else {
                  labelHeader.text = "Medical Support"
              }
      
              headerView.backgroundColor = UIColor.blue;
              headerView.addSubview(labelHeader) //Add UILabel on HeaderView
              return headerView
      
          default:
              assert(false, "Unexpected element kind")
          }
      }
      

      【讨论】:

        【解决方案4】:

        这是我的解决方案。它的完成类似于为 indexPathAt 将单元格/行出列。

        CollectionView

        // size of header
        func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize{
            CGSize(width: collectionView.frame.size.width, height: 123)
        }
        
        // content of header
        func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
            guard let headerCell = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: CollectionViewHeader.identifier, for: indexPath) as? CollectionViewHeader else {
                return UICollectionReusableView()
            }
            headerCell.titleLabel.text = "Title"
            return headerCell
        }
        

        UICollectionReusableView 子类

        class CollectionViewHeader: UICollectionReusableView {
            let identifier = "headerView"
            @IBOutlet weak var titleLabel: UILabel!
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-08-29
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多