Using custom rows in a list
The number of lines of code required to display items in a List
view row could vary from one to several lines of code. Repeating the code several times or in several places increases the chance of an error occurring and potentially becomes very cumbersome to maintain. One change would require updating the code in several different locations or files.
A custom list row can be used to solve this problem. This custom row can be written once and used in several places, thereby improving maintainability and encouraging reuse.
Let’s find out how to create custom list rows.
Getting ready
Let’s start by creating a new SwiftUI app named CustomRows
.
How to do it…
We will reorganize the code in our static lists to make it more modular. We’ll create a separate file to hold the WeatherInfo
struct, a separate SwiftUI file for the custom view, WeatherRow
, and finally, we’ll implement the components in the ContentView...