Another solution instead of tableView.reloadData()

前端 未结 2 1093
时光说笑
时光说笑 2021-01-24 04:55

I am calling batch updates(begin/end updates) on a tableView using this code:

  let oldImageSet = Set(oldImageArray)
  let newImageSet = Set(self.images)
  let m         


        
相关标签:
2条回答
  • 2021-01-24 05:44

    You should just apply what you're doing with rows to sections which means changing IndexPaths to IndexSets. IndexSet can be initialized with an array of Ints where each Int represents a corresponding section.

      let oldImageSet = Set(oldImageArray)
      let newImageSet = Set(self.images)
      let addedImages = newImageSet.subtracting(oldImageSet)
      // map each image to the section equivalent to its index in `self.images`
      let addedImageSections = Array(missingImages).map{ self.images.indexOf($0)! }
      let addedImageIndexSet = IndexSet(addedImageSections)
    
      // repeat above process but in reverse to find images that have been removed
      let removedImages = oldImageSet.subtracting(newImageSet)
      let removedImageSections = Array(removedImages).map{ oldImageArray.index(of: $0)! }
      let removedImageIndexSet = IndexSet(removedImageSections)
    
      self.tableView.beginUpdates()
    
      // if images have been added insert new sections 
      if !addedImageIndexSet.isEmpty {
            self.tableView.insertSections(addedImageIndexSet, with: .top)
      }
    
      // if images have been deleted remove sections    
      if !removedImageIndexSet.isEmpty {
            self.tableView.deleteSections(removedImageIndexSet, with: .none)
      }
      self.tableView.endUpdates()
    
    0 讨论(0)
  • 2021-01-24 05:49
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 55
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
    
        return posts.count
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }
    

    replace code with following code :

    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 55
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
    
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return posts.count
    }
    
    0 讨论(0)
提交回复
热议问题