Static and Dynamic List Content with SwiftUI
Creating a data-driven list with SwiftUI requires much less code than using a tableView or collectionView along with their dataSource and delegate methods. For example:
But what if we also wanted static content within the list, alongside our dynamic content?
With a tableView or collectionView, this would require logic within a datasource method (likely an if or switch statement on the indexPath). And hold the risk of an off-by-one error.
With SwiftUI, however, this becomes much less verbose. The dynamic list is simply implemented as a ForEach within the List declaration. And the static content is added alongside it. For example, if we wanted to add a static button among our dynamic content:
Yet another way that declarative syntax of SwiftUI makes it much simpler to create complex list UI’s.