gpt4 book ai didi

ios - 如何解决收藏 View 闪烁问题? ( swift )

转载 作者:可可西里 更新时间:2023-10-31 23:44:30 28 4
gpt4 key购买 nike

我正在开发一个使用 Parse 作为后端服务器的 Collection View 项目。并且不太确定为什么我的 Collection View 每次出现在屏幕上时都会闪烁。 Collection View 的第二个单元格在 View 出现在屏幕上后一秒钟让我闪烁,第三个单元格的图像。那么,你们能帮我解决这个问题吗?这是我当前的文件看起来像..

override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)

if PFUser.currentUser() == nil {
// the user hasn't logged in yet
showLogin()
} else {
// the user logged in, do something else
fetchInterests()

let center = NSNotificationCenter.defaultCenter()
let queue = NSOperationQueue.mainQueue()
center.addObserverForName("NewInterestCreated", object: nil, queue: queue, usingBlock: { (notification) -> Void in

if let newInterest = notification.userInfo?["newInterestObject"] as? Interest {
if !self.interestWasDisplayed(newInterest) {
self.interests.insert(newInterest, atIndex: 0)
self.collectionView.reloadData()
}
}
})

}

func interestWasDisplayed(newInterest: Interest) -> Bool {
for interest in interests {
if interest.objectId! == newInterest.objectId! {
return true
}
}
return false



override func viewDidLoad() {
super.viewDidLoad()



// Do any additional setup after loading the view.
if UIScreen.mainScreen().bounds.size.height == 480.0 {
let flowLayout = self.collectionView.collectionViewLayout as! UICollectionViewFlowLayout
flowLayout.itemSize = CGSizeMake(250.0, 300.0)
}

configureUserProfile()



func fetchInterests()
{
let currentUser = User.currentUser()!
let interestIds = currentUser.interestIds
if interestIds?.count > 0
{
let interestQuery = PFQuery(className: Interest.parseClassName())
interestQuery.orderByDescending("updatedAt")
interestQuery.cachePolicy = PFCachePolicy.NetworkElseCache
interestQuery.whereKey("objectId", containedIn: interestIds)

interestQuery.findObjectsInBackgroundWithBlock({ (objects, error) -> Void in
if error == nil {
if let interestObjects = objects as [PFObject]? {
self.interests.removeAll()
for interestObject in interestObjects {
let interest = interestObject as! Interest
self.interests.append(interest)
}
self.collectionView.reloadData()
}
} else {
print("\(error!.localizedDescription)")
}
})
}

最佳答案

在 viewDidAppear 中,您在 collectionView 上调用 reloadData 的次数太多了。确保 block 中的重新加载数据在主线程上被调用。

关于ios - 如何解决收藏 View 闪烁问题? ( swift ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33140226/

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