Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios Swift-创建不带序列图像板initWithFrame错误的collectionView_Ios_Swift_Uicollectionview - Fatal编程技术网

Ios Swift-创建不带序列图像板initWithFrame错误的collectionView

Ios Swift-创建不带序列图像板initWithFrame错误的collectionView,ios,swift,uicollectionview,Ios,Swift,Uicollectionview,我正在创建一个没有故事板的collectionView-将其从我的项目中删除 我在这里创建它,在AppDelegate中。我的页脚和页眉已正确出列,加载图像时显示的单元格为零,但我无法确定在何处调用此方法initWithFrame!谢谢你的帮助 不使用情节提要 调用内部应用程序委托 let window = UIWindow(frame: UIScreen.main.bounds) let myTabBar = TabBarVC() window.rootViewController = myT

我正在创建一个没有故事板的collectionView-将其从我的项目中删除

我在这里创建它,在AppDelegate中。我的页脚和页眉已正确出列,加载图像时显示的单元格为零,但我无法确定在何处调用此方法initWithFrame!谢谢你的帮助

不使用情节提要

调用内部应用程序委托

let window = UIWindow(frame: UIScreen.main.bounds)
let myTabBar = TabBarVC()
window.rootViewController = myTabBar
window.makeKeyAndVisible()
let homeVC = HomeVC(collectionViewLayout: UICollectionViewFlowLayout())
myTabBar.present(homeVC, animated: false)
这最终会失败,出现以下错误:

2018-06-26 16:02:15.379473-0700 MMDH[62033:2946934] -[MMDH.HomeVC 
initWithFrame:]: unrecognized selector sent to instance 0x7fdcce82f400
2018-06-26 16:02:15.389976-0700 MMDH[62033:2946934] *** Terminating app due to 
uncaught exception 'NSInvalidArgumentException', reason: '-[MMDH.HomeVC 
initWithFrame:]: unrecognized selector sent to instance 0x7fdcce82f400
我的单元格、页脚和页眉都已退出队列。我不知道这是哪里出的错。。。以下是设置代码:

class HomeVC: UICollectionViewController, UICollectionViewDelegateFlowLayout, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

var headerVC = HeaderView()
var footerVC = homeFooterView()

var photos = [Photo]()
var imageFetchOperationQueue = OperationQueue()
var infoOperationQueue = OperationQueue()

let width = UIScreen.main.bounds.width
let height = UIScreen.main.bounds.height
let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()

let cache = NSCache<NSString, UIImage>()

override func viewDidLoad() {
    super.viewDidLoad()

    layout.itemSize = CGSize(width: (width / 3), height: (width / 3))
    layout.sectionInset = UIEdgeInsets(top: 1, left: 1, bottom: 1, right: 1)
    layout.headerReferenceSize = CGSize(width: width, height: width / 2)
    layout.minimumInteritemSpacing = 1
    layout.minimumLineSpacing = 1

    if photos.count == 0 {
        layout.footerReferenceSize = CGSize(width: width, height: width)
    } else if photos.count < 9 {
        layout.footerReferenceSize = CGSize(width: 0, height: 0)
    } else {
        layout.footerReferenceSize = CGSize(width: 0, height: 0)
    }
    collectionView!.collectionViewLayout = layout

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

    collectionView?.register(HomeVC.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "homeVC")
    self.view.addSubview(headerVC)
    collectionView?.register(homeFooterView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionFooter, withReuseIdentifier: "footerVC")
    self.view.addSubview(footerVC)
    collectionView?.register(HomePicCell.self, forCellWithReuseIdentifier: "Cell")


}
}
类HomeVC:UICollectionViewController、UICollectionViewDelegateFlowLayout、UIImagePickerController delegate、UINavigationController delegate{
var headerVC=HeaderView()
var footerVC=homeFooterView()
var photos=[Photo]()
var imageFetchOperationQueue=OperationQueue()
var infoOperationQueue=OperationQueue()
让宽度=UIScreen.main.bounds.width
让高度=UIScreen.main.bounds.height
let布局:UICollectionViewFlowLayout=UICollectionViewFlowLayout()
let cache=NSCache()
重写func viewDidLoad(){
super.viewDidLoad()
layout.itemSize=CGSize(宽度:(宽度/3),高度:(宽度/3))
layout.sectionInset=UIEdgeInSet(顶部:1,左侧:1,底部:1,右侧:1)
layout.headerReferenceSize=CGSize(宽度:宽度,高度:宽度/2)
layout.minimumInteritemSpacing=1
layout.minimumLineSpacing=1
如果photos.count==0{
layout.footerReferenceSize=CGSize(宽度:宽度,高度:宽度)
}否则,如果照片数量小于9{
layout.footerReferenceSize=CGSize(宽度:0,高度:0)
}否则{
layout.footerReferenceSize=CGSize(宽度:0,高度:0)
}
collectionView!.collectionViewLayout=布局
collectionView=UICollectionView(框架:self.view.frame,collectionViewLayout:layout)
collectionView?.dataSource=self
collectionView?.delegate=self
collectionView?.register(HomeVC.self,用于种类:UICollectionElementKindSectionHeader的SupplementViews,带有ReuseIdentifier:“HomeVC”)
self.view.addSubview(headerVC)
collectionView?.register(homeFooterView.self,用于种类为UICollectionElementKindSectionFooter的SupplementViews,带有ReuseIdentifier:“footerVC”)
self.view.addSubview(footerVC)
collectionView?.register(HomePicCell.self,强制使用ReuseIdentifier:“单元”)
}
}

您的
viewDidLoad
方法中存在一些问题

  • 您的主要问题是将
    HomeVC
    类注册为标题视图类型。您想使用
    HeaderView.self
    ,而不是
    HomeVC.self
  • 不要根据屏幕大小进行布局。您的收藏视图可能不会占据整个屏幕。将布局基于集合视图的大小。还请注意,大小可能会发生变化。最好在
    viewWillTransition
    方法中更新布局
  • 这是一个UICollectionViewController。您不应该创建自己的
    UICollectionView
    。这将在调用
    viewDidLoad
    之前为您完成。删除创建集合视图的三行,并设置
    数据源
    委托

  • 次要,但您的
    homeFooterView
    类应命名为
    homeFooterView
    。类名应该以大写字母开头。

    您的
    viewDidLoad
    方法中存在一些问题

  • 您的主要问题是将
    HomeVC
    类注册为标题视图类型。您想使用
    HeaderView.self
    ,而不是
    HomeVC.self
  • 不要根据屏幕大小进行布局。您的收藏视图可能不会占据整个屏幕。将布局基于集合视图的大小。还请注意,大小可能会发生变化。最好在
    viewWillTransition
    方法中更新布局
  • 这是一个UICollectionViewController。您不应该创建自己的
    UICollectionView
    。这将在调用
    viewDidLoad
    之前为您完成。删除创建集合视图的三行,并设置
    数据源
    委托

  • 次要,但您的
    homeFooterView
    类应命名为
    homeFooterView
    。类名应该以大写字母开头。

    Hey@rmaddy,我已经尝试了你的解决方案,但仍然不起作用-得到相同的错误。我遵循了这里的指南,这是有效的(设置相同)。这可能是因为我没有初始化某个标签或uiimageview?听起来HomeVC本身并没有使用框架进行初始化。所以您将行
    collectionView?.register更改为
    collectionView?.register(HomeVC.self,用于种类:uicollectionlementkindsectionheader,withReuseIdentifier:“HomeVC”)
    collectionView?.register(HeaderView.self,用于种类的SupplementViewofKind:UICollectionElementKindSectionHeader,带有ReuseIdentifier:“homeVC”)
    ?如果您这样做了,但仍然存在问题,请使用堆栈跟踪更新您的问题,以显示崩溃发生的位置。这些更改是在与UICollectionReusableView交互时发生的。除此之外,我已使我的所有代码正常工作。我已尝试在此处添加方法:对发生这种情况的原因有何想法?似乎无法理解框架可以工作,这只是当我使用页眉/页脚时…它们也是空的。你没有回复我之前的评论。嘿@rmaddy,我尝试了你的解决方案,但仍然不起作用-得到相同的错误。我遵循了这里的指南,这是有效的(设置相同)。这可能是由于我没有初始化某个标签或uiimageview造成的吗?听起来HomeVC本身没有使用框架进行初始化..?因此您更改了行
    collectionView?.register(HomeVC.self,用于类的SupplementViews:UICollectionElementKindSectionHeader,withReuseIdentifier:“HomeVC”)
    collectionView?.register(HeaderView.self,用于实物的辅助视图:UICollecti