我在CollectionView
内使用UIViewController
,此CollectionView
使用PinterestLayout。我现在已经有10个项目(1个是" +"图像和9个礼物图像)。当我点击此saveButton
再添加一个Gift
:
它不会加载" Torres"图像。
以下是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 :(得分:1)
如果要刷新collectionView中的数据,则必须在主线程中重新加载数据。
dispatch_async(dispatch_get_main_queue(),{
collectionView.reloadData()
})