Call batch updates with TableView

血红的双手。 提交于 2020-01-07 01:53:09

问题


With a collection I would perform batch updates to prevent it from flashing...Now I'm trying to do the same with a table, if possible...

In a previous post I did it as the answer shows....CollectionView flash when reloadData

here's my query, as I think I need to add it in there:

let query = PFQuery(className: "Posts")
query.limit = self.page
query.addDescendingOrder("createdAt")
query.whereKey("username", notContainedIn: self.followArray)
query.whereKey("username", notEqualTo: PFUser.currentUser()!.username!)
query.findObjectsInBackgroundWithBlock ({ (objects:[PFObject]?, error:NSError?) -> Void in

    if error == nil {
        //let oldUUIDArray = self.uuidArray
        self.images.removeAll(keepCapacity: false)
        self.tableData.removeAll(keepCapacity: false)


        for object in objects! {

            if let _ = object.valueForKey("textPost") as? PFFile {
                // this must be a text post
                let post = ImageStruct(dataTypeInit: 0,
                    uuidInit: object.valueForKey("uuid") as! String,
                    textInit: object.valueForKey("textPost") as! PFFile)


               self.images.append(post)
            } else {
                // this must be an image
                    // this must be a text post
                    let post = ImageStruct(dataTypeInit: 1,
                        uuidInit: object.valueForKey("uuid") as! String,
                        imageInit: object.valueForKey("image") as! PFFile)


                    self.images.append(post)
            }}
        self.tableView1.reloadData()
    }

If anyone can help me change it from batch updates to the tableView version that would be great....


回答1:


let query = PFQuery(className: "Posts")
query.limit = self.page
query.addDescendingOrder("createdAt")
query.whereKey("username", notContainedIn: self.followArray)
query.whereKey("username", notEqualTo: PFUser.currentUser()!.username!)
query.findObjectsInBackgroundWithBlock ({ (objects:[PFObject]?, error:NSError?) -> Void in

    if error == nil {
        let oldImageArray = self.images
        self.images.removeAll(keepCapacity: false)
        self.tableData.removeAll(keepCapacity: false)
        let oldImageArray = self.images

        for object in objects! {

            if let _ = object.valueForKey("textPost") as? PFFile {
                // this must be a text post
                let post = ImageStruct(dataTypeInit: 0,
                    uuidInit: object.valueForKey("uuid") as! String,
                    textInit: object.valueForKey("textPost") as! PFFile)


               self.images.append(post)
            } else {
                // this must be an image
                    // this must be a text post
                    let post = ImageStruct(dataTypeInit: 1,
                        uuidInit: object.valueForKey("uuid") as! String,
                        imageInit: object.valueForKey("image") as! PFFile)


                    self.images.append(post)
            }}
        let newImageUUIDArray = self.images.map({return $0.uuid})
        let oldImageUUIDArray = oldImageArray.map({return $0.uuid})
        let oldImageSet = Set(oldImageUUIDArray)
        let newImageSet = Set(newImageUUIDArray)
        let missingImages = newImageSet.subtract(oldImageSet)

        let missingImageIndexPaths = Array(missingImages).map{NSIndexPath(forRow:newImageUUIDArray.indexOf($0)!,inSection:0)}

        let removedImages = oldImageSet.subtract(newImageSet)
        let removedImageIndexPaths = Array(removedImages).map{NSIndexPath(forRow:oldImageUUIDArray.indexOf($0)!,inSection:0)}



     self.tableView1.beginUpdate()
            if missingImageIndexPath.count != 0 {
       self.tableView1.insertRowsAtIndexPaths(missingImageIndexPaths,withRowAnimation:.Right)
}
            if removedImageIndexPath.count != 0 {
              self.tableView1.deleteRowsAtIndexPaths(removedImageIndexPaths,withRowAnimation:.Fade)
}
            self.tableView1.endUpdates()
        }

You could factor the determining of new image and deleted image index paths into other functions to make it more readable. But in general this will only update rows that have changed rather than reloading all of the data.



来源:https://stackoverflow.com/questions/36043809/call-batch-updates-with-tableview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!