List
- UIKit
UITableView
과 유사
ForEach
와 주로 함께 사용
ForEach
의 3가지 사용 방법
1. ForEach(_ data: id: content:)
struct LocationInfo: Hashable {
var city = ""
var weather = ""
}
...
List() {
ForEach(locations, id:\.self) { location in
HStack {
Text("\(location.city)")
Text("\(location.weather)")
}
}
}
...
- model: Hashable 필수
- \.self \.weather \.city 중 하나를 id로 설정 필요
2. ForEach(_ data: content:)
struct LocationInfo: Identifiable {
var id = UUID()
var city = ""
var weather = ""
}
...
List() {
ForEach(locations) { location in
HStack {
Text("\(location.city)")
Text("\(location.weather)")
}
}
}
...
- model: Identifiable 필수
- 고유 ID 프로퍼티 필요 (UUID() 사용)
3. ForEach(_ data:Range<Int> content:)
struct LocationInfo {
var city = ""
var weather = ""
}
...
List() {
ForEach( 0..<locations.count, id: \.self ) { row in
HStack {
Text("\(row + 1)")
Text("\(locations[row].city)")
Text("\(locations[row].weather)")
}
}
}
...