ios collectionView:reloadData 有效,reloadItemsAtIndexPaths 无效
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27930987/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
collectionView: reloadData works, reloadItemsAtIndexPaths does not
提问by Le Toucan Sale
I have a UICollectionView
whose source data changes sometimes when offscreen. To update it when it returns, I wrote in the CollectionViewController
:
我有一个UICollectionView
源数据在屏幕外有时会发生变化。为了在它返回时更新它,我在CollectionViewController
:
override func viewWillAppear(animated: Bool) {
self.collectionView!.reloadData()
}
...and that works, but I never need to update more than one or two cells in my UICollectionView
, so I thought it'd be better if I replaced the above version with:
...这是可行的,但我从来不需要更新超过一两个单元格UICollectionView
,所以我认为如果我用以下版本替换上面的版本会更好:
override func viewWillAppear(animated: Bool) {
self.collectionView!.reloadItemsAtIndexPaths(myArrayOfIndexPaths)
}
...but when I do, Xcode gives me an error saying:
...但是当我这样做时,Xcode 给了我一个错误说:
Operand of postfix '!' should have optional type, delete '!'".
后缀 '!' 的操作数 应该有可选类型,删除'!'”。
When I delete the '!', it says that UICollectionView
doesn't have a member named reloadItemsAtIndexPaths
. What am I doing wrong?
当我删除“!”时,它说UICollectionView
没有名为reloadItemsAtIndexPaths
. 我究竟做错了什么?
回答by Valentin
From your code it looks like you have declared your collectionView as Optional (with ?) at the end and maybe linked it to your storyboard using an @IBOutlet. To fix your issue you should remove the ? from the :
从您的代码来看,您似乎在最后将 collectionView 声明为 Optional(带有 ?),并且可能使用 @IBOutlet 将其链接到您的故事板。要解决您的问题,您应该删除 ? 来自 :
@IBOutlet var collectionView: UICollectionView?
@IBOutlet var collectionView: UICollectionView?
and substitute it with:
并将其替换为:
@IBOutlet var collectionView: UICollectionView!
@IBOutlet var collectionView: UICollectionView!
this way you are telling the compiler that your collectionView definitely exists (because you have linked it from your storyboard).
这样你就告诉编译器你的 collectionView 肯定存在(因为你已经从你的故事板链接了它)。
Alternatively, you can bind your collectionView by doing this:
或者,您可以通过执行以下操作来绑定 collectionView:
if let collectionView = collectionView {
collectionView.reloadItemsAtIndexPaths(myArrayOfIndexPaths)
}
回答by Vac
Swift 4reload all items in section
Swift 4重新加载部分中的所有项目
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
var indexPaths: [NSIndexPath] = []
for i in 0..<collectionView!.numberOfItems(inSection: 0) {
indexPaths.append(NSIndexPath(item: i, section: 0))
}
collectionView?.reloadItems(at: indexPaths as [IndexPath])
}
回答by mriaz0011
Swift 3 version code
Swift 3 版本代码
var indexPaths = [IndexPath]()
indexPaths.append(indexPath) //"indexPath" ideally get when tap didSelectItemAt or through long press gesture recogniser.
collectionView.reloadItems(at: indexPaths)
回答by karique
as a beginner i didn't know that there's a diference when you implement a collectionView as a direct child from another viewController and implement it as an embedded view inside a viewController, maybe some dev is facing the same problem and they doesn't know, if so, look for the correct approach :)
作为初学者,我不知道将 collectionView 作为另一个 viewController 的直接子级实现并将其实现为 viewController 内的嵌入式视图时存在差异,也许某些开发人员面临同样的问题,他们不知道,如果是这样,请寻找正确的方法:)
回答by Nizzam
If you want to reload for single cell, you can specify the specific of the cell like this :-
如果要重新加载单个单元格,可以像这样指定单元格的具体内容:-
for index in indexPaths {
if index == [0, 0] {
print(index)
self.collectionView?.reloadItems(at: [index])
}
}
Hope this helps.
希望这可以帮助。