gpt4 book ai didi

swift - collectionView.reloadData 无法正常运行

转载 作者:行者123 更新时间:2023-11-30 13:05:22 25 4
gpt4 key购买 nike

我发现我的 followCollectionView 没有在我的代码中这个非常特定的位置重新加载。

func unFollowBtnPressed() {

if let itemPaths = followCollectionView.indexPathsForSelectedItems() {
unFollowPosts(itemPaths)
} else {
// No item to unfollow
}

self.followCollectionView.reloadData()
}

func unFollowPosts(itemPaths: [NSIndexPath]) {

followCollectionView.performBatchUpdates({

let sortedIndexPathArray = itemPaths.sort{$1.row < $0.row}

for indexP in sortedIndexPathArray {
DataService.ds.removeFollow(PostService.ps.followingPosts[indexP.row])
PostService.ps.followingPosts.removeAtIndex(indexP.row)
}
self.followCollectionView.deleteItemsAtIndexPaths(sortedIndexPathArray)

}) { (success: Bool) in
}
}

程序执行reloadData(),但cellForItemAtIndexPath从未被调用(一切都正确连接,因为其他地方调用reloadData工作正常)。我认为这是一个计时问题,因为如果我重新安排重新加载数据的位置, Collection View 就能够重新加载。下面是工作代码。我不确定为什么上面的方法有效而下面的方法不起作用。我尝试将 reloadData() 代码包装在dispatch_async(dispatch_get_main_queue()) 中,但它仍然不起作用?

func unFollowBtnPressed() {

if let itemPaths = followCollectionView.indexPathsForSelectedItems() {
unFollowPosts(itemPaths)
} else {
// No item to unfollow
self.followCollectionView.reloadData()
}

}

func unFollowPosts(itemPaths: [NSIndexPath]) {

followCollectionView.performBatchUpdates({

let sortedIndexPathArray = itemPaths.sort{$1.row < $0.row}

for indexP in sortedIndexPathArray {
DataService.ds.removeFollow(PostService.ps.followingPosts[indexP.row])
PostService.ps.followingPosts.removeAtIndex(indexP.row)
}
self.followCollectionView.deleteItemsAtIndexPaths(sortedIndexPathArray)

}) { (success: Bool) in
self.followCollectionView.reloadData()
}
}

最佳答案

你尝试过这些吗:

DispatchQueue.main.async(execute: {
//reload
})

OperationQueue.main.addOperation({
//reload
})

关于swift - collectionView.reloadData 无法正常运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39449747/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com