Share
Embed
Collection Update
Demo how UITableView, UICollection performs batch update
Khoa Pham
November 27, 2017
Transcript
33 slides
-
index vs offset Array(0..<10).enumerated().forEach { (offset, element) in } -
https://en.wikipedia.org/wiki/Zero-based_numbering - offset from the starting position -
NSInternalInconsistencyException Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid
update: invalid number of items in section 0. The number of items contained in an existing section after the update (213) must be equal to the number of items contained in that section before the update (154), plus or minus the number of items inserted or deleted from that section (40 inserted, 0 deleted) and plus or minus the number of items moved into or out of that section (0 moved in, 0 moved out).' -
Game of IndexPath —https://github.com/onmyway133/ CollectionUpdateExample —10 quizzes items = ["a",
"b", "c", "d", "e", "f"] -
1) insert 3 items at the end items.append(contentsOf: ["g", "h",
"i"]) // a, b, c, d, e, f, g, h, i let indexPaths = Array(6...8).map { IndexPath(item: $0, section: 0) } collectionView.insertItems(at: indexPaths) -
2) delete 3 items at the end items.removeLast() items.removeLast() items.removeLast()
// a, b, c let indexPaths = Array(3...5).map { IndexPath(item: $0, section: 0) } collectionView.deleteItems(at: indexPaths) -
3) update item at index 2 items[2] = " !
" // a, b, ! , d, e, f let indexPath = IndexPath(item: 2, section: 0) collectionView.reloadItems(at: [indexPath]) -
4) move item "c" to the end items.remove(at: 2) items.append("c")
// a, b, d, e, f, c collectionView.moveItem( at: IndexPath(item: 2, section: 0), to: IndexPath(item: 5, section :0) ) -
5) delete 3 items at the beginning, then insert 3
items at the end items.removeFirst() items.removeFirst() items.removeFirst() items.append(contentsOf: ["g", "h", "i"]) // d, e, f, g, h, i collectionView.performBatchUpdates({ let deleteIndexPaths = Array(0...2).map { IndexPath(item: $0, section: 0) } collectionView.deleteItems(at: deleteIndexPaths) let insertIndexPaths = Array(3...5).map { IndexPath(item: $0, section: 0) } collectionView.insertItems(at: insertIndexPaths) }, completion: nil) -
6) insert 3 items at the end, then delete 3
items beginning items.append(contentsOf: ["g", "h", "i"]) items.removeFirst() items.removeFirst() items.removeFirst() // d, e, f, g, h, i collectionView.performBatchUpdates({ let insertIndexPaths = Array(6...8).map { IndexPath(item: $0, section: 0) } collectionView.insertItems(at: insertIndexPaths) let deleteIndexPaths = Array(0...2).map { IndexPath(item: $0, section: 0) } collectionView.deleteItems(at: deleteIndexPaths) }, completion: nil) -
! Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'attempt
to insert item 6 into section 0, but there are only 6 items in section 0 after the update' -
6) insert 3 items at the end, then delete 3
items beginning ! items.append(contentsOf: ["g", "h", "i"]) items.removeFirst() items.removeFirst() items.removeFirst() // d, e, f, g, h, i collectionView.performBatchUpdates({ let deleteIndexPaths = Array(0...2).map { IndexPath(item: $0, section: 0) } collectionView.deleteItems(at: deleteIndexPaths) let insertIndexPaths = Array(3...5).map { IndexPath(item: $0, section: 0) } collectionView.insertItems(at: insertIndexPaths) }, completion: nil) -
Operations insertItems(at indexPaths: [IndexPath]) deleteItems(at indexPaths: [IndexPath]) reloadItems(at indexPaths: [IndexPath])
moveItem(at indexPath: IndexPath, to newIndexPath: IndexPath) performBatchUpdates(_ updates, completion) insertSections(_ sections: IndexSet) deleteSections(_ sections: IndexSet) reloadSections(_ sections: IndexSet) moveSection(_ section: Int, toSection newSection: Int) -
Deletions "k" -> "" ! 1 deletion "ki" -> ""
! 2 deletions "kit" -> "" ! 3 deletions -
Insertions "" -> "k" ! 1 insertion "" -> "ka"
! 2 insertions "" -> "kat" ! 3 insertions -
horizonally: insertion vertically: deletion diagonally: substitution If not equal, check
minimum value from left, top, top left. Then increase by one -
It equals again, take value from top left. Bottom right
value gives edit distance (number of steps) "kit" -> "kat" ! Substitute "i" with "a" -
Q & A Thanks for listening. Hope you have a
great time