简体   繁体   English

如何仅在更新时刷新collectionview或tableview中的数据?

[英]How to refresh the data in collectionview or tableview only for the update?

How can i only refresh or reload newly added data in collectionview or tableview instead of reload all the data? 如何仅刷新或重新加载CollectionView或TableView中新添加的数据,而不重新加载所有数据? Right now i am using following code but it query the database sever to get all the data then reload everything. 现在我正在使用以下代码,但是它查询数据库服务器以获取所有数据,然后重新加载所有内容。 For example i upload only one photo on the sever so i just want to get that photo and reload collectionview only for this newly added photo not load everyting. 例如,我在服务器上仅上传了一张照片,因此我只想获取该照片并仅针对此新添加的照片重新加载collectionview,而不会加载所有内容。 how can i do that? 我怎样才能做到这一点? Thank you! 谢谢!

func loadPosts(){

    let query = PFQuery(className: "posts")
    query.whereKey("username", equalTo:PFUser.current()!.username!) 
    query.limit = page
    query.findObjectsInBackground { (objects, error) in

        if error == nil {
            self.picArray.removeAll(keepingCapacity: false)

         if let objects  = objects{ 
            for object in objects{
                self.picArray.append(object.value(forKey: "pic") as! PFFile)
            }
          }
            self.collectionView?.reloadData()
        } else{
            print(error!.localizedDescription)
        }
    }
 }

Collection view function 收藏集查看功能

override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return picArray.count
    }

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        //define cell

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! pictureCell

        picArray[indexPath.row].getDataInBackground { (data, error) in

            if error == nil {
                cell.picImg.image = UIImage(data: data!)
            } else {
                print(error!.localizedDescription)
            }
        }

        return cell
    }

Eventually I would recommend moving the image request out of cellForItem because it will get called a lot depending on user scrolling behavior, but for now that will work. 最终,我建议将图像请求移出cellForItem因为它会根据用户的滚动行为而被调用很多,但是现在可以了。

You will want to check if the image has already been loaded then use reloadItems instead of reloadData so your new section in cellForItem will look something like: 您将要检查的图像已经被加载,然后使用reloadItems代替reloadData所以在您的新栏目cellForItem看起来是这样的:

if (cell.picImg.image == nil) {
    picArray[indexPath.row].getDataInBackground { (data, error) in

        if error == nil {
            cell.picImg.image = UIImage(data: data!)
            collectionView.reloadItems(at: indexPath) //reload only this item once the image is retrieved
        } else {
            print(error!.localizedDescription)
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM