IOS list diff library Dwifft example usage
1. Install by adding this in your cocoapods Podflie and run pod install from the command line window in your project directory.
pod 'Dwifft'
2. In order to use the Dwifft library, we need to use SectionedValues and TableViewDiffCalculator provided by the Dwifft library. We need to put wrap the data in SectionedValues and retrieve the data from the TableViewDiffCalculator.
3. Here is a sample data wrapped in the SectionValues.
let data = [ ("foods", ["Onions","Pineapples",]), ("animal-related", ["Cats","A used lobster","Fish legs","Adam's apple",]), ("muddy things", ["Mud",]), ("other", ["Splinters","Igloo cream","Self-flying car"]) ] let sectionedValues = SectionedValues(data)
4. Here is a sample TableViewDiffCalculator for the above data.
var diffCalculator: TableViewDiffCalculator?
5. Whenever a new set of data is available, we just assign the SectionedValues to the TableViewDiffCalculator’s sectionedValues, and the TableViewDiffCalculator will take care of all the updates needed in the TableView. The data source functions for the tableview must use this diffCalculator to retrieve the list data.
diffCalculator.sectionedValues = sectionedValues
6. We must use the diffCalculator to get the numberOfSections.
override func numberOfSections(in tableView: UITableView) -> Int { return self.diffCalculator?.numberOfSections() ?? 0 }
7. We must use the diffCalculator to get the numberOfObjects for each section.
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return self.diffCalculator?.numberOfObjects(inSection: section) ?? 0 }
8. We must use the diffCalculator to get the data for each cell.
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier", for: indexPath) cell.textLabel?.text = self.diffCalculator?.value(atIndexPath: indexPath) return cell }
9. We must use the diffCalculator to get the data for the header as well.
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { return self.diffCalculator?.value(forSection: section) }
10. Last but not least, if data object is a custom object other than String, it has to implement the protocol Equatable. The demo above uses String, since String already conforms to Equatable, hence it doesn’t need to implement the protocol Equatable.
Search within Codexpedia
Search the entire web