uicollectionview在reloaddata之后立即选择一个项目?

las*_*mer 13 ios uicollectionview

调用-[UICollectionView reloadData]后需要一些时间才能显示单元格,因此在调用后立即选择项目reloadData不起作用.有没有办法立即选择一个项目reloadData

use*_*021 8

根据这个答案,我发现在我对collectionView执行其他操作之前,调用layoutIfNeededafter reloadData似乎有效地"刷新"重新加载:

[self.collectionView reloadData];
[self.collectionView layoutIfNeeded];
...
Run Code Online (Sandbox Code Playgroud)

在页面上我找到了这个解决方案,一些评论者表示它在iOS 9上不适用于他们,但它对我来说没问题所以你的里程可能会有所不同.


Ars*_*nik 7

斯威夫特方式:

let selected = collectionView.indexPathsForSelectedItems()
collectionView.performBatchUpdates({ [weak self] in
    self?.collectionView.reloadSections(NSIndexSet(index: 0))
    }) { completed -> Void in
        selected?.forEach { [weak self] indexPath in
            self?.collectionView.selectItemAtIndexPath(indexPath, animated: false, scrollPosition: [])
        }
}
Run Code Online (Sandbox Code Playgroud)