티스토리 뷰
예제 코드
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
import SwiftUI
struct Animal: Identifiable{
let id = UUID()
let name: String
let index: Int
}
struct ContentView: View {
var animalList = [
Animal(name: "dog", index: 3),
Animal(name: "dog", index: 1),
Animal(name: "dog", index: 2),
Animal(name: "cat", index: 7),
Animal(name: "cat", index: 5),
Animal(name: "bird", index: 4),
Animal(name: "bird", index: 6)
]
var animalDictionary: [String:[Animal]]{
var dictionaryData = Dictionary(grouping: animalList){ $0.name }
for (key,value) in dictionaryData {
dictionaryData[key] = value.sorted(by: {
$0.index < $1.index
})
}
return dictionaryData
}
var animalDictionaryKeys: [String] {
animalDictionary.map( { $0.key } )
}
var body: some View {
List{
ForEach(animalDictionaryKeys, id: \.self){ key in
Section(header: Text("\(key)")){
ForEach(animalDictionary[key]!){ animal in
HStack{
Text("\(animal.name)")
Text("\(animal.index)")
Text("\(animal.id)")
}
}
}
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
Group {
ContentView()
}
}
}
|
cs |
실행 결과
학습 개념
struct Animal : UUID(), identifiable
animalDictionary : sort
List : ForEach 구문, closer 활용, Section 활용
'iOS 개발 > SwiftUI' 카테고리의 다른 글
SwiftUI) placeholder 사용 예제 (0) | 2022.01.28 |
---|---|
SwiftUI) Grid 사용 예제 (0) | 2022.01.28 |
SwiftUI) WebView 사용하기 예제 - UIKit으로 만든 웹뷰 가져오기 예제 (0) | 2022.01.28 |
SwiftUI) button alert 예제 (0) | 2022.01.23 |
SwiftUI) ViewModifer 개념 예제 (0) | 2022.01.23 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- XcodeBuildSystem
- 알고리즘
- Swift
- 생명 주기
- XCFramework
- 순환참조
- swiftc
- clean code 정리
- CLANG
- dfs
- 클린 코드 정리
- BFS
- 이분탐색
- 클린 코드
- 주입
- 단어변환
- 프로그래머스
- 의존성
- SwiftUI
- clean code
- 메모리 순환참조
- 전처리기
- ios
- 여행경로
- 의존관계역전법칙
- 클린 코드 줄거리
- 면접질문
- 링커
- ios simulator
- 학교 과제
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
글 보관함