无法将项目附加到CollectionView

时间:2016-04-22 08:34:37

标签: swift collectionview

我在CollectionView内使用UIViewController,此CollectionView使用PinterestLayout。我现在已经有10个项目(1个是" +"图像和9个礼物图像)。当我点击此saveButton再添加一个Gift

enter image description here

它不会加载" Torres"图像。

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

}

1 个答案:

答案 0 :(得分:1)

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

dispatch_async(dispatch_get_main_queue(),{
   collectionView.reloadData()
})
相关问题