Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mohsinaliayub/indexedtableview
A table view which displays index on the trailing side of the screen, allowing easy navigation to sections.
https://github.com/mohsinaliayub/indexedtableview
Last synced: 6 days ago
JSON representation
A table view which displays index on the trailing side of the screen, allowing easy navigation to sections.
- Host: GitHub
- URL: https://github.com/mohsinaliayub/indexedtableview
- Owner: mohsinaliayub
- Created: 2022-04-15T10:45:11.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2022-04-15T11:03:43.000Z (almost 3 years ago)
- Last Synced: 2024-11-19T17:57:13.862Z (2 months ago)
- Language: Swift
- Size: 1.19 MB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# IndexedTableView
A table view which displays index on the trailing side of the screen, allowing easy navigation to sections.# Description
When dealing with a large number of records, it is simple and effective to organize data into sections and provide an index list for easy access.
We populated the data using a **UITableViewDiffableDataSource** and used its sectionIndexTitles(for: UITableView) method to provide sections.Here is the code that displays section title, index title and index to the section for easy access.
```swift
class AnimalTableDataSource: UITableViewDiffableDataSource {
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
snapshot().sectionIdentifiers[section]
}override func sectionIndexTitles(for tableView: UITableView) -> [String]? {
snapshot().sectionIdentifiers
}override func tableView(_ tableView: UITableView, sectionForSectionIndexTitle title: String, at index: Int) -> Int {
index
}
}
```Just use this custom diffable data source class to create cells and populate data.
```swift
let dataSource = AnimalTableDataSource(tableView: tableView) { tableView, indexPath, animalName in
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)// configure the cell
cell.textLabel?.text = animalName// ...
return cell
}
tableView.dataSource = dataSource
```Finally, you will need to create a snapshot, append sections and items to it, and apply the snapshot to the above data source. ```animalsDict``` is
a dictionary of type ```[String: String]``` and ```animalSectionTitles``` is an array of String.
```swiftvar snapshot = NSDiffableDataSourceSnapshot()
snapshot.appendSections(animalSectionTitles) // append our sections
animalSectionTitles.forEach { section in
if let animals = animalsDict[section] {
snapshot.appendItems(animals, toSection: section) // for each section, add the items corresponding to that section.
}
}dataSource.apply(snapshot)
```
# Screenshots
Here's how the app looks.