使用TableView调用批量更新

时间:2016-03-16 18:15:33

标签: swift uitableview

使用集合我会执行批量更新以防止它闪烁......现在我正在尝试使用表格,如果可能的话......

在上一篇文章中,我做了答案显示...... CollectionView flash when reloadData

这是我的查询,因为我认为我需要在那里添加它:

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

如果有人可以帮助我将其从批量更新更改为tableView版本,那就太棒了....

1 个答案:

答案 0 :(得分:0)

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

您可以将新图像和已删除图像索引路径的确定因素考虑到其他函数中,以使其更具可读性。但一般情况下,这只会更新已更改的行,而不是重新加载所有数据。