2016-04-22 50 views
2

我在UIViewController中使用CollectionView,此CollectionView使用PinterestLayout。我现在已经有10件商品(1件是“+”图片和9件礼品图片)。当我点击这个saveButton多加一个Gift不能将项目追加到CollectionView

enter image description here

它不会加载“托雷斯”的形象。

enter image description here

这是我在saveButton行动代码:

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 

} 
+0

那么你的'UICollectionViewDataSource'代码呢?似乎有一个问题 –

+0

我已经添加了'UICollectionViewDataSource'方法..请再次检查。谢谢。 – Khuong

+0

看起来没问题。你确定那些'UICollectionViewDataSource'是在你添加新的'Gift'后调用的吗? –

回答

1

当你想在你必须做的CollectionView刷新数据重新加载主线程中的数据。

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

它不起作用。 – Khuong

+0

我需要更多的代码来帮助你。如果你可以放你的UIViewController代码,它可能会很棒。谢谢 –

+0

好的。我刚刚添加了它们。请再检查一次。谢谢。 – Khuong

相关问题