如何解决集合视图闪烁问题? (迅速)

时间:2015-10-15 05:02:30

标签: ios swift uicollectionview xcode7

我正在处理一个使用Parse作为后端服务器的集合视图项目。并且不太清楚为什么我的收藏视图每次出现在屏幕上时都会闪烁..收集视图的第二个单元格在视图出现在屏幕上后第二个单元格闪烁,第三个单元格的图像。那么,你们能帮助我解决这个问题吗?这是我当前的文件看起来像..

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)")
        }
    })
}

1 个答案:

答案 0 :(得分:0)

就像在viewDidAppear中一样,您在collectionView上调用reloadData的次数过多。 确保在主线程上调用了块中的重载数据。

相关问题