加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

swift3 – Swift 3 UICollectionView可重用节标题将不会显示

发布时间:2020-12-14 04:52:08 所属栏目:百科 来源:网络整理
导读:在使用 Xcode 8.2的swift 3中,我无法使用UICollectionView显示reusableHeaderView.我在标题视图文件的不同位置放置断点,它们永远不会被触发.这是我的代码. import UIKitclass WishListViewController: UIViewController,UICollectionViewDelegateFlowLayout,
在使用 Xcode 8.2的swift 3中,我无法使用UICollectionView显示reusableHeaderView.我在标题视图文件的不同位置放置断点,它们永远不会被触发.这是我的代码.

import UIKit

class WishListViewController: UIViewController,UICollectionViewDelegateFlowLayout,UICollectionViewDataSource {

var collectionView: UICollectionView!

var wishListItems = [[String:Any]]()

var shops = [[String: Any]]()

let cellId = "WishListCell"

let headerId = "WishListHeaderView"

var screenSize: CGRect!
var screenWidth: CGFloat!
var screenHeight: CGFloat!

let imageAspectRatio: CGFloat = 1.2

override func viewDidLoad() {

    super.viewDidLoad()

    screenSize = UIScreen.main.bounds
    screenWidth = screenSize.width
    screenHeight = screenSize.height

    let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
    layout.sectionInset = UIEdgeInsets(top: 0,left: 0,bottom: 0,right: 0)
    layout.itemSize = CGSize(width: screenWidth,height: ((screenWidth / 3) * imageAspectRatio) + 20)
    layout.minimumInteritemSpacing = 0
    layout.minimumLineSpacing = 0
    layout.estimatedItemSize.height = ((screenWidth / 3) * imageAspectRatio) + 20
    layout.estimatedItemSize.width = screenWidth

    collectionView = UICollectionView(frame: self.view.frame,collectionViewLayout: layout)

    collectionView.delegate = self

    collectionView.dataSource = self

    collectionView.register(WishListCell.self,forCellWithReuseIdentifier: cellId)

    collectionView.register(WishListHeaderView.self,forSupplementaryViewOfKind: UICollectionElementKindSectionHeader,withReuseIdentifier: headerId)

    collectionView.backgroundColor = UIColor.white

    self.view.addSubview(collectionView)

    // Do any additional setup after loading the view.
}

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

    switch kind {

    case UICollectionElementKindSectionHeader:

        let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind,withReuseIdentifier: headerId,for: indexPath) as! WishListHeaderView

        headerView.textLabel.text = shops[indexPath.section]["shop_name"] as? String

        return headerView

    default:

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

这是我的标题视图文件.

import UIKit

class WishListHeaderView: UICollectionReusableView {

var textLabel: UILabel

let screenWidth = UIScreen.main.bounds.width

override init(frame: CGRect) {

    textLabel = UILabel()

    super.init(frame: frame)

    self.addSubview(textLabel)

    textLabel.font = UIFont.systemFont(ofSize: UIFont.smallSystemFontSize)

    textLabel.textAlignment = .left

    textLabel.numberOfLines = 0

    textLabel.lineBreakMode = NSLineBreakMode.byWordWrapping

    textLabel.translatesAutoresizingMaskIntoConstraints = false

    NSLayoutConstraint.activate([


        NSLayoutConstraint(item: textLabel,attribute: .leading,relatedBy: .equal,toItem: self,attribute: .leadingMargin,multiplier: 1.0,constant: 0.0),NSLayoutConstraint(item: textLabel,attribute: .trailing,attribute: .trailingMargin,])

    self.backgroundColor = .white

}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}
}

解决方法

您需要为集合视图实现标头功能的高度.

func collectionView(_ collectionView: UICollectionView,layout collectionViewLayout: UICollectionViewLayout,referenceSizeForHeaderInSection section: Int) -> CGSize

然后返回你想要的高度.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读