Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.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
Swift 无法将项目附加到CollectionView_Swift_Collectionview - Fatal编程技术网

Swift 无法将项目附加到CollectionView

Swift 无法将项目附加到CollectionView,swift,collectionview,Swift,Collectionview,我正在UIViewController内部使用CollectionView,此CollectionView使用PinterestLayout。我现在已经有10件物品(1件是“+”图片,9件礼物图片)。当我单击此保存按钮再添加一份礼物: 它不会加载“托雷斯”图像 以下是我在save按钮中的代码操作: func saveButtonTapped(sender: AnyObject) { addViewIsShowed = false let caption = a

我正在
UIViewController
内部使用
CollectionView
,此
CollectionView
使用PinterestLayout。我现在已经有10件物品(1件是“+”图片,9件礼物图片)。当我单击此
保存按钮
再添加一份
礼物

它不会加载“托雷斯”图像

以下是我在
save按钮中的代码
操作:

func saveButtonTapped(sender: AnyObject) {
        addViewIsShowed = false
        let caption = addView.addTextView.text
        let image = addView.addImageView.image!
        let gift = Gift(caption: caption, image: image.decompressedImage)
        // gifts.count is 10
        gifts.append(gift)
        // gifts.count is now 11
        addView.frame = CGRect(x: 0, y: 0, width: 0, height: 0)
        collectionView.alpha = 1.0
        saveButton!.removeFromSuperview()
        collectionView.reloadData()
    }
下面是CollectionViewDataSource方法:

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
   return gifts.count
}

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

   let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! GiftCollectionViewCell
   cell.gift = gifts[indexPath.item]
   return cell

}

如果要刷新collectionView中的数据,必须在主线程中重新加载数据

dispatch_async(dispatch_get_main_queue(),{
   collectionView.reloadData()
})

您的
UICollectionViewDataSource
代码如何?似乎有问题,我添加了
UICollectionViewDataSource
方法。。请再查一下。谢谢,看起来还可以。你确定那些
UICollectionViewDataSource
在你添加新的
Gift
后会被调用吗?是的,我在那里设置了debug并调用了它们。当我更改
gifts.append(Gift)
gifts.insert(Gift,atIndex:1)
,它会追加并显示torres图像,但是删除了
collectionView
上的图像9。我需要更多的代码来帮助您。如果你能把你的UIViewController代码放进去,那就太棒了。谢谢。我刚刚添加了它们。请再查一下。谢谢。正如我所说,当我更改
礼物。将(礼物)
附加到
礼物。插入(礼物,索引:1)
,它会附加并显示torres图像,但会删除collectionView上的图像9。所以应该调用重载数据。我认为问题在于PinterestLayout。