替代tableView.reloadData()的另一种解决方案 [英] Another solution instead of tableView.reloadData()

查看:214
本文介绍了替代tableView.reloadData()的另一种解决方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码在tableView上调用批处理更新(开始/结束更新):

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

  let oldImageSet = Set(oldImageArray)
  let newImageSet = Set(self.images)
  let missingImages = newImageSet.subtracting(oldImageSet)

  let missingImageIndexPaths = Array(missingImages).map{NSIndexPath(row: self.images.index(of: $0)!, section: 0)}
  //Array(missingImages).map{NSIndexPath(forRow:newImageUUIDArray.indexOf($0)!,inSection:1)}

  let removedImages = oldImageSet.subtracting(newImageSet)
  let removedImageIndexPaths = Array(removedImages).map{NSIndexPath(row:oldImageArray.index(of: $0)!,section:0)}

  self.tableView.beginUpdates()
  if missingImageIndexPaths.isEmpty == false {
        self.tableView.insertRows(at: missingImageIndexPaths as [IndexPath],with:.top)
  }

  if removedImageIndexPaths.isEmpty == false {
        self.tableView.deleteRows(at: removedImageIndexPaths as [IndexPath],with:.none)
  }
  self.tableView.endUpdates()

哪个工作正常?

修改

这是我的tableView部分的计数以及如何设置它:

This is my tableView section counts and how I have it set up:

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
}

但是现在我想调用另一个tableView,但是它具有"Sections",所以当我使用它时会崩溃...?

But now I have another tableView I would like to be able to call this on, But it has 'Sections' so When I use this I get a crash...?

有人知道如何在带有部分的tableView上使用此代码吗?

Does anyone know how to use this code on a tableView with sections?

非常感谢!

推荐答案

您应该将对行的操作应用于部分,这意味着将IndexPath s更改为IndexSet s.可以用Int s数组初始化IndexSet,其中每个Int代表一个对应的节.

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

这篇关于替代tableView.reloadData()的另一种解决方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆