Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/111.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 通过UICollectionView中的NIB添加自定义单元格_Ios_Swift_Uicollectionview_Nib - Fatal编程技术网

Ios 通过UICollectionView中的NIB添加自定义单元格

Ios 通过UICollectionView中的NIB添加自定义单元格,ios,swift,uicollectionview,nib,Ios,Swift,Uicollectionview,Nib,我有一个非常简单的swift程序,在UICollectionView中使用一个Nib构建的自定义单元格 当我运行应用程序时,我得到一个错误声明 :由于未捕获异常“NSInternalInconsistencyException”而终止应用程序,原因:“无法在捆绑包中加载NIB:” 我已经看过所有教程,并且完全遵循了它们,看起来我不知怎么地缺少了一个初始值设定项,但我不知所措。在故事板编辑器中CollectionViewController的主默认单元格中,我将重用标识符命名为“cell”,对于N

我有一个非常简单的swift程序,在UICollectionView中使用一个Nib构建的自定义单元格

当我运行应用程序时,我得到一个错误声明

:由于未捕获异常“NSInternalInconsistencyException”而终止应用程序,原因:“无法在捆绑包中加载NIB:”

我已经看过所有教程,并且完全遵循了它们,看起来我不知怎么地缺少了一个初始值设定项,但我不知所措。在故事板编辑器中CollectionViewController的主默认单元格中,我将重用标识符命名为“cell”,对于Nib(swift文件名为CustomCollectionViewCell.xib),我将重用标识符命名为“我在Xib中有一个单独的UIButton作为一个操作,如果按下它,应该会记录成功

这里是主要的例子:

class MainCollectionViewController: UICollectionViewController {

override func viewDidLoad() {
    super.viewDidLoad()

    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = false


    // Register cell classes
    self.collectionView!.registerNib(UINib(nibName: "CustomCell", bundle: nil), forCellWithReuseIdentifier: "Cell")

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

override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return 1
}


override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of items
    return 1
}

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! CustomCollectionViewCell

    // Configure the cell

    return cell
}
}
以下是CustomCollectionViewCell代码:

class CustomCollectionViewCell: UICollectionViewCell {

override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
}

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
}


@IBAction func testButton(sender: UIButton) {
    print("Success!")
}
}


我知道这是一个基本的初学者项目,但我在网上到处都找过了,我被难住了。如果有任何帮助,我将不胜感激。

为什么不使用原型单元格。将原型单元格的类别更改为CustomCollectionViewCell,在该单元格中设置您的UI。删除:

self.collectionView!.registerNib(UINib(nibName: "CustomCell", bundle: nil), forCellWithReuseIdentifier: "Cell")

在ViewDiLoad and delete CustomCollectionViewCell.xib中,我忘了在上面的主VC中添加我确实定义了let reuseIdentifier=“Cell”。我已经做到了这一点,但我希望能够在collection视图中有不同的单元格,每个单元格都有自己的UI项(即,一个可能有一个UI,另一个可能有一个UI),除了唯一的XIB文件之外,我不知道如何做。您可以有多个具有不同单元格标识符的原型单元格和UICollectionViewCell的不同子类。我使用多个原型单元格工作,比使用XIB容易得多。我仍然好奇为什么上面的代码不起作用。我认为您的标识符错了。我很抱歉lieve你不需要在tableview中有任何原型单元格,然后用唯一的ID注册你的NIB。就是这样!我消除了原型单元格,一切都正常了。