ios – 如何在swift中创建可扩展的集合视图?

ios – 如何在swift中创建可扩展的集合视图?,第1张

概述我有2个集合视图部分,当第一次加载此VC时,两个部分(室内和室外)最初将只显示3个项目. 但是在用户按下“更多”按钮后,室内或室外的每个部分都会展开并显示所有可用项目,如下图所示 我试图制作代码,但有时它似乎可以扩展,有时它不会扩展(仍显示3项). 这是主控制器中的代码: class FacilitiesVC: UIViewController { @IBOutlet weak var

我有2个集合视图部分,当第一次加载此VC时,两个部分(室内和室外)最初将只显示3个项目.

但是在用户按下“更多”按钮后,室内或室外的每个部分都会展开并显示所有可用项目,如下图所示

我试图制作代码,但有时它似乎可以扩展,有时它不会扩展(仍显示3项).

这是主控制器中的代码:

class FacilitIEsVC: UIVIEwController {    @IBOutlet weak var collectionVIEw: UICollectionVIEw!    var facilitIEscategoryData = [[String:Any]]()    var outdoorFacilitIEsIsExpanded = false    var indoorFacilitIEsIsExpanded = false    overrIDe func vIEwDIDLoad() {        super.vIEwDIDLoad()        getIndoorOutdoorFacilitIEsData()    }}extension FacilitIEsVC {    // MARK: - Helper Methods    func getIndoorOutdoorFacilitIEsData() {        let facilitIEsData = FacilitIEscategorylibrary.fetchFacilitIEscategory()        var indoorFacilitIEs = [FacilitIEscategory]()        var outdoorFacilitIEs = [FacilitIEscategory]()        // distinguishing between indoor and outdoor data        for facdata in facilitIEsData {            if facdata.type == "Indoor Facility" {                indoorFacilitIEs.append(facdata)            } else {                outdoorFacilitIEs.append(facdata)            }        }        facilitIEscategoryData = [            ["Title": "Indoor FacilitIEs","info": indoorFacilitIEs],["Title": "Outdoor FacilitIEs","info": outdoorFacilitIEs]        ]    }}extension FacilitIEsVC: UICollectionVIEwDataSource {    // MARK: - UICollectionVIEwDataSource    func numberOfSections(in collectionVIEw: UICollectionVIEw) -> Int {        return facilitIEscategoryData.count    }    func collectionVIEw(_ collectionVIEw: UICollectionVIEw,numberOfItemsInSection section: Int) -> Int {        if section == 0 {            if indoorFacilitIEsIsExpanded {                let category = facilitIEscategoryData[section]                let infoList = category["info"] as! [FacilitIEscategory]                return infoList.count            } else {                return 3            }        } else {            if outdoorFacilitIEsIsExpanded {                let category = facilitIEscategoryData[section]                let infoList = category["info"] as! [FacilitIEscategory]                return infoList.count            } else {                return 3            }        }    }    func collectionVIEw(_ collectionVIEw: UICollectionVIEw,cellForItemAt indexPath: IndexPath) -> UICollectionVIEwCell {        let cell = collectionVIEw.dequeueReusableCell(withReuseIDentifIEr: StoryBoard.facilitIEscategoryCellIDentifIEr,for: indexPath) as! FacilitIEsCell        let category = facilitIEscategoryData[indexPath.section]        let infoList = category["info"] as! [FacilitIEscategory]        cell.facilitIEscategoryData = infoList[indexPath.item]        return cell    }    // for section header vIEw    func collectionVIEw(_ collectionVIEw: UICollectionVIEw,vIEwForSupplementaryElementOfKind kind: String,at indexPath: IndexPath) -> UICollectionReusableVIEw {        let sectionheaderVIEw = collectionVIEw.dequeueReusableSupplementaryVIEw(ofKind: kind,withReuseIDentifIEr: StoryBoard.facilitIEsSectionheaderIDentifIEr,for: indexPath) as! FacilitIEsSectionheader        let category = facilitIEscategoryData[indexPath.section]        sectionheaderVIEw.categoryData = category        sectionheaderVIEw.sectionheaderDelegate = self        return sectionheaderVIEw    }}extension FacilitIEsVC: FacilitIEsSectionheaderDelegate {    func dIDPressbutton(_ facilitIEs: String,isExpanded: Bool) {        if facilitIEs == "Indoor FacilitIEs" {            indoorFacilitIEsIsExpanded = isExpanded        } else if facilitIEs == "Outdoor FacilitIEs" {            outdoorFacilitIEsIsExpanded = isExpanded        }        collectionVIEw.reloadData()    }}

这是集合视图部分标题中的代码

import UIKitprotocol FacilitIEsSectionheaderDelegate: class {    func dIDPressbutton(_ facilitIEs: String,isExpanded: Bool)}class FacilitIEsSectionheader: UICollectionReusableVIEw {    @IBOutlet weak var TitleLabel: UILabel!    weak var sectionheaderDelegate: FacilitIEsSectionheaderDelegate?    var collectionIsExpanded = false    var facilitIEsType = ""    var categoryData: [String:Any]! {        dIDSet {            TitleLabel.text = categoryData["Title"] as? String            facilitIEsType = categoryData["Title"] as! String        }    }    @IBAction func morebuttonDIDpressed(_ sender: Any) {        collectionIsExpanded = !collectionIsExpanded        sectionheaderDelegate?.dIDPressbutton(facilitIEsType,isExpanded: collectionIsExpanded)    }}

也许我在这行代码中收集numberOfItemsInSection中的错误

func collectionVIEw(_ collectionVIEw: UICollectionVIEw,numberOfItemsInSection section: Int) -> Int {    if section == 0 {        if indoorFacilitIEsIsExpanded {            let category = facilitIEscategoryData[section]            let infoList = category["info"] as! [FacilitIEscategory]            return infoList.count        } else {            return 3        }    } else {        if outdoorFacilitIEsIsExpanded {            let category = facilitIEscategoryData[section]            let infoList = category["info"] as! [FacilitIEscategory]            return infoList.count        } else {            return 3        }    }}

我认为该section参数与indexPath.section相同,但似乎有所不同,但我不知道如何从collection numberOfItemsInSection方法访问indexPath.section

怎么解决这个问题?

解决方法 您必须在vIEwForSupplementaryElementOfKind中分配当前状态

func collectionVIEw(_ collectionVIEw: UICollectionVIEw,at indexPath: IndexPath) -> UICollectionReusableVIEw {    let sectionheaderVIEw = collectionVIEw.dequeueReusableSupplementaryVIEw(ofKind: kind,for: indexPath) as! FacilitIEsSectionheader    let category = facilitIEscategoryData[indexPath.section]    sectionheaderVIEw.categoryData = category    sectionheaderVIEw.sectionheaderDelegate = self    if(indexPath.section == 0)    {       sectionheaderVIEw.collectionIsExpanded =  indoorFacilitIEsIsExpanded    }    else    {      sectionheaderVIEw.collectionIsExpanded = outdoorFacilitIEsIsExpanded    }    return sectionheaderVIEw}

在FacilitIEsSectionheader内部,你可以根据它进行翻转

@IBAction func morebuttonDIDpressed(_ sender: Any) {    collectionIsExpanded = !collectionIsExpanded    sectionheaderDelegate?.dIDPressbutton(facilitIEsType,isExpanded: collectionIsExpanded)}
总结

以上是内存溢出为你收集整理的ios – 如何在swift中创建可扩展的集合视图?全部内容,希望文章能够帮你解决ios – 如何在swift中创建可扩展的集合视图?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1033249.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-24
下一篇 2022-05-24

发表评论

登录后才能评论

评论列表(0条)

保存