UICollectionView 单元格与 Header 重叠 [英] UICollectionView cell overlapping with Header

查看:35
本文介绍了UICollectionView 单元格与 Header 重叠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) ->CG尺寸{让 collectionViewWidth = collectionView.bounds.width让键 = Array(indoordataDictionary.keys)让 keyString:String = 键[indexPath.section]让 objFacilityUserList:FacilityUserList = 室内数据字典[keyString]!作为!设施用户列表如果 objFacilityUserList.facilityUserList.count == 1 {return CGSize(width: collectionViewWidth, height: 30)}返回CGSize(宽度:collectionViewWidth/2,高度:70)}func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) ->CGFloat {返回 0}func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) ->CGFloat {返回 5}func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) ->CG尺寸{如果室内段.selectedSegmentIndex == 1 {返回 CGSize(width:collectionView.frame.size.width, height:0.0)}别的 {返回 CGSize(width:collectionView.frame.size.width, height:40.0)}}func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) ->UICollectionReusableView {让 headerView: PlaceCollectionReusableView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: TagIDConstant.nibs.kPlaceCollectionReusableViewID, for: indexPath) as!PlaceCollectionReusableView让键 = Array(indoordataDictionary.keys)让 keyString:String = 键[indexPath.section]headerView.placeNameLabel.text = keyString返回标题视图}

解决方案

如果您正在使用 CollectionView 的自定义布局,那么以下链接是最好的类,它将解决本节标题重叠问题.

https://gist.github.com/olxios/9e0ae85d49529b0d8b8a60>fabae

希望对那些使用 CollectionView 自定义布局的人有所帮助.

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    let collectionViewWidth = collectionView.bounds.width


        let keys = Array(indoordataDictionary.keys)
        let keyString:String = keys[indexPath.section]
        let objFacilityUserList:FacilityUserList = indoordataDictionary[keyString]! as! FacilityUserList

        if objFacilityUserList.facilityUserList.count == 1 {
            return CGSize(width: collectionViewWidth, height: 30)
        }
    return CGSize(width: collectionViewWidth/2, height: 70)
}


func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
    return 0
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
    return 5
}

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

    if indoorSegment.selectedSegmentIndex == 1 {
        return CGSize(width:collectionView.frame.size.width, height:0.0)
    }
    else {
        return CGSize(width:collectionView.frame.size.width, height:40.0)
    }
}


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

    let headerView: PlaceCollectionReusableView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: TagIDConstant.nibs.kPlaceCollectionReusableViewID, for: indexPath) as! PlaceCollectionReusableView
    let keys = Array(indoordataDictionary.keys)
    let keyString:String = keys[indexPath.section]
    headerView.placeNameLabel.text = keyString

    return headerView
}

解决方案

If you are using the Custom Layout for the CollectionView then the following link is the best class for that, which will solve this Section Header overlapping problem.

https://gist.github.com/olxios/9e0ae85d49529b0d8b8a6fe6e7a00fab

Hope it helps to those who are using the Custom Layout for the CollectionView.

这篇关于UICollectionView 单元格与 Header 重叠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆