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。