Reputation:
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()
Which is working alright.
Edit
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
}
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...?
Does anyone know how to use this code on a tableView with sections?
Many thanks in advance!
Upvotes: 1
Views: 176
Reputation: 15331
You should just apply what you're doing with rows to sections which means changing IndexPath
s to IndexSet
s. IndexSet
can be initialized with an array of Int
s 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()
Upvotes: 0
Reputation: 328
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
}
Upvotes: 1