Giter VIP home page Giter VIP logo

indexedtableview's Introduction

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.

class AnimalTableDataSource: UITableViewDiffableDataSource<String, String> {
    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.

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.

var snapshot = NSDiffableDataSourceSnapshot<String, String>()
    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.

Indexed Table View

indexedtableview's People

Contributors

mohsinaliayub avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.