uicollectionview – 嵌入在Navigation Controller中时,CollectionView不起作用

uicollectionview – 嵌入在Navigation Controller中时,CollectionView不起作用,第1张

概述我有一个简单的集合视图测试(基于在线教程),它独立工作.但是当我将它嵌入导航控制器时,它就会停止工作.我在代码中构建了屏幕:(1)创建一个headerView(64像素高)并将其添加到顶部的视图中. (2)我构建了一个集合视图并将其添加到headerView中. 这是代码: import UIKitclass ViewController: UIViewController,UICollec 我有一个简单的集合视图测试(基于在线教程),它独立工作.但是当我将它嵌入导航控制器时,它就会停止工作.我在代码中构建了屏幕:(1)创建一个headerVIEw(64像素高)并将其添加到顶部的视图中. (2)我构建了一个集合视图并将其添加到headerVIEw中.

这是代码:

import UIKitclass VIEwController: UIVIEwController,UICollectionVIEwDelegate,UICollectionVIEwDataSource,UINavigationControllerDelegate{var collectionVIEw : UICollectionVIEw!var topVIEw: UIVIEw!overrIDe func vIEwDIDLoad() {    super.vIEwDIDLoad()    var frame = CGRect(x:0,y:128,wIDth:vIEw.frame.wIDth,height:64)    topVIEw = UIVIEw(frame:frame)    self.vIEw.addSubvIEw(topVIEw)    // CollectionVIEw    let layout: UICollectionVIEwFlowLayout = UICollectionVIEwFlowLayout()    layout.scrollDirection = .horizontal    layout.sectionInset = UIEdgeInsets(top: 0,left: 10,bottom: 0,right: 10)    layout.itemSize = CGSize(wIDth: 50,height: 50)    frame = CGRect(x: 0,y: 0,wIDth: Int(self.topVIEw.frame.wIDth),height: Int(self.topVIEw.frame.height))    collectionVIEw = UICollectionVIEw (frame: frame,collectionVIEwLayout: layout)    collectionVIEw.dataSource = self    collectionVIEw.delegate = self    collectionVIEw.register(UICollectionVIEwCell.self,forCellWithReuseIDentifIEr: "collectionCell")    collectionVIEw.backgroundcolor = UIcolor.green    self.topVIEw.addSubvIEw(collectionVIEw)}//MARK: - CollectionVIEwfunc collectionVIEw(_ collectionVIEw: UICollectionVIEw,numberOfItemsInSection section: Int) -> Int {    return 14}func collectionVIEw(_ collectionVIEw: UICollectionVIEw,cellForItemAt indexPath: IndexPath) -> UICollectionVIEwCell {    let cell = collectionVIEw.dequeueReusableCell(withReuseIDentifIEr: "collectionCell",for: indexPath as IndexPath)    for v in cell.subvIEws {        v.removeFromSupervIEw()    }    cell.backgroundcolor = UIcolor.orange    let label = UILabel(frame: CGRect(x:0,y:0,wIDth:50,height:50))    label.text = "\(indexPath.item)"    label.textAlignment = .center    label.textcolor = UIcolor.white    cell.addSubvIEw(label)    return cell}func numberOfSections(in collectionVIEw: UICollectionVIEw) -> Int {    return 1}overrIDe func dIDReceiveMemoryWarning() {    super.dIDReceiveMemoryWarning()    // dispose of any resources that can be recreated.}

}

解决方法 如上所述,我无法使考查尔的建议工作.它确实给了我一个线索,我对嵌入集合视图的视图的定位在vIEwDIDLoad中被错放了,原因我还没理解.但是,通过将集合视图配置放在vIEwDIDAppear(而不是vIEwDIDLoad)中,它运行良好.我将y位置偏移64以清除导航栏,并将行高减少到64.我还将代码只执行一次,以便从页面导航不会添加多个彼此顶部的视图.顺便说一句,我最初的目标是水平滚动细胞.在我的程序中,我有相应部分的tablevIEw,并且想法是使用具有水平滚动单元格的行移动到相应的部分.

代码如下所示:

////  CustomVIEwController.swift//  DSM Tracker////  Created by Syed Tariq on 1/7/17.//  copyright © 2017 com.syedtariq. All rights reserved.//import UIKitclass VIEwController: UIVIEwController,UINavigationControllerDelegate{    var executeOnce = true    var cellDimensions = [String:Int]()    var cellHeight = 50    var cellWIDth = 120    var collectionContainerVIEw: UICollectionVIEw!    var navbar: UINavigationbar = UINavigationbar()    // vIEw constants    var vIEwY = CGfloat()    var vIEwX = CGfloat()    var vIEwWIDth = CGfloat()    var vIEwHeight = CGfloat()    // gaps from vIEw edge    let leftGap = CGfloat(20)    let rightGap = CGfloat(20)    // navbar constants    let navbarHeight = CGfloat(64)    var headerLabels = ["Cell 01","Cell 02","Cell 03","Cell 04","Cell 05","Cell 06","Cell 07","Cell 08","Cell 09","Cell 10","Cell 11","Cell 12","Cell 13","Cell 14","Cell 15","Cell 16"]    overrIDe func vIEwDIDLoad() {        super.vIEwDIDLoad()        navbar.backgroundcolor = UIcolor.green        executeOnce = true        vIEwY = vIEw.frame.origin.y        vIEwX = vIEw.frame.origin.x        vIEwWIDth = vIEw.frame.wIDth        vIEwHeight = vIEw.frame.height    }    func configureCollectionVIEw () {        if executeOnce {            executeOnce = false            let layout: UICollectionVIEwFlowLayout = UICollectionVIEwFlowLayout()            layout.scrollDirection = .horizontal            layout.sectionInset = UIEdgeInsets(top: 0,left: 0,right: 10)            layout.itemSize = CGSize(wIDth: cellWIDth,height: cellHeight)            let colWIDth = vIEwWIDth - leftGap - rightGap            let colX = vIEwX + leftGap            let colY = vIEwY + navbarHeight            let colHeight = CGfloat(64)            let frame = CGRect(x:colX,y:colY,wIDth: colWIDth,height: colHeight)            //let frame = CGRect.zero            collectionContainerVIEw = UICollectionVIEw (frame: frame,collectionVIEwLayout: layout)            collectionContainerVIEw.dataSource = self            collectionContainerVIEw.delegate = self            collectionContainerVIEw.autoresizingMask = [.flexibleleftmargin,.flexibleleftmargin,.flexibleBottommargin,.flexibleRightmargin,.flexibleHeight,.flexibleWIDth]            collectionContainerVIEw.register(UICollectionVIEwCell.self,forCellWithReuseIDentifIEr: "collectionCell")            collectionContainerVIEw.backgroundcolor = UIcolor.blue            collectionContainerVIEw.allowsSelection = true            collectionContainerVIEw.isScrollEnabled = true            collectionContainerVIEw.setNeedsdisplay()            print("collectionContainerVIEw.frame \(collectionContainerVIEw.frame)")            vIEw.addSubvIEw(collectionContainerVIEw)        }    }    overrIDe func vIEwDIDAppear(_ animated: Bool) {        configureCollectionVIEw()    }    func numberOfSections(in collectionVIEw: UICollectionVIEw) -> Int {        return 1    }    func collectionVIEw(_ collectionVIEw: UICollectionVIEw,numberOfItemsInSection section: Int) -> Int {        print("headerLabels.count \(headerLabels.count)")        return  headerLabels.count    }    func collectionVIEw(_ collectionVIEw: UICollectionVIEw,cellForItemAt indexPath: IndexPath) -> UICollectionVIEwCell {        let cell = collectionVIEw.dequeueReusableCell(withReuseIDentifIEr: "collectionCell",for: indexPath as IndexPath)        for v in cell.subvIEws {            v.removeFromSupervIEw()        }        let cellTitle = headerLabels[indexPath.row]        let cellTitlelines = cellTitle.components(separatedBy: " ")        let nLabels = cellTitlelines.count        cell.layer.borderWIDth = 1        cell.layer.cornerRadius = 8        let labelHeight = cellHeight / cellTitlelines.count        for i in (0 ..< nLabels) {            let frame = CGRect(x: 0,y: labelHeight * i,wIDth: cellWIDth,height: labelHeight)            let label1 = UILabel(frame: frame)            cell.backgroundcolor = UIcolor.lightGray            label1.numberOflines = 1            label1.text = headerLabels[indexPath.row]            label1.textAlignment = .center            label1.textcolor = UIcolor.black            label1.clipsToBounds = true            label1.adjustsFontSizetoFitWIDth = true            label1.text = cellTitlelines[i]            cell.addSubvIEw(label1)        }        return cell    }    func collectionVIEw(_ collectionVIEw: UICollectionVIEw,shouldSelectItemAt indexPath: IndexPath) -> Bool {        collectionContainerVIEw.scrollToItem(at:IndexPath(item: indexPath.item,section: 0),at: .centeredHorizontally,animated: false)        return true    }    func collectionVIEw(_ collectionVIEw: UICollectionVIEw,dIDSelectItemAt indexPath: IndexPath) {        let cell = collectionContainerVIEw.cellForItem(at: indexPath)        print("cell = \(cell)")        collectionContainerVIEw.scrollToItem(at:IndexPath(item: indexPath.item,animated: false)    }    func collectionVIEw(_ collectionVIEw: UICollectionVIEw,shouldHighlightItemAt indexPath: IndexPath) -> Bool {        return true    }    func collectionVIEw(_ collectionVIEw: UICollectionVIEw,dIDHighlightItemAt indexPath: IndexPath) {    }    overrIDe func prepare(for segue: UIStoryboardSegue,sender: Any?) {    }}
总结

以上是内存溢出为你收集整理的uicollectionview – 嵌入在Navigation Controller中时,CollectionView不起作用全部内容,希望文章能够帮你解决uicollectionview – 嵌入在Navigation Controller中时,CollectionView不起作用所遇到的程序开发问题。

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

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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存