List
ビューは、データのリストを表示するためのViewです。
目次
Listビューの使い方
List {
子View1
子View2
子View3
・・・
}
単純にListの中にViewを列挙していくだけで、一覧を表示することができます。
struct ContentView: View {
var body: some View {
List {
Text("りんご")
Text("バナナ")
Text("みかん")
Text("メロン")
Text("さくらんぼ")
}
}
}
![](https://seeds-digital.com/wp-content/uploads/2023/12/30c8f87e5f8c3ed693fb105d20e4fec0-1024x508.png)
配列データをListに入れて表示する場合
データベースから取得したデータの一覧などをListに並べたい次のようにForEachを使います。
struct ContentView: View {
// Listに入れるデータ一覧
let fruits = ["りんご", "オレンジ", "バナナ"]
var body: some View {
List {
ForEach(fruits, id: \.self) { fruit in
Text(fruit)
}
}
}
}
ForEachを使わずに表現することも可能
List
自体が ForEach
を内部で使用しているので、次のように書くこともできます。
struct ContentView: View {
let fruits = ["りんご", "オレンジ", "バナナ"]
var body: some View {
List(fruits, id: \.self) { fruit in
Text(fruit)
}
}
}
NavigationLinkをつける
必要に応じて、リスト内の要素をタップしたときのアクションや、詳細画面へのナビゲーションなどを追加できます。
struct ContentView: View {
let fruits = ["りんご", "オレンジ", "バナナ", "メロン", "もも"]
var body: some View {
NavigationStack {
List(fruits, id: \.self) { fruit in
NavigationLink(destination: Text("\(fruit)の詳細")) {
HStack {
Text(fruit)
Spacer()
}
}
}
.navigationTitle("果物リスト")
}
}
}
![](https://seeds-digital.com/wp-content/uploads/2023/12/94484c9f5c93f18941790facfdb7ae9f-507x1024.png)
![](https://seeds-digital.com/wp-content/uploads/2023/12/3a42abe571a47543f8e8339aa0a04d1d-1-507x1024.png)
Listの表示設定
List
は、さまざまな表示スタイルをカスタマイズするためのモディファイアを提供しています。以下は、よく使用される List
の表示スタイルのモディファイアについての解説です。
.listStyle(GroupedListStyle())
:
GroupedListStyle
は、iOSの標準的なグループ化されたリストスタイルを提供します。
セクションごとにグループ分けされ、各セクションにはヘッダーやフッターが含まれます。
struct ContentView: View {
let items = [
("果物", ["りんご", "バナナ"]),
("野菜", ["にんじん", "キャベツ"]),
]
var body: some View {
NavigationStack {
List {
ForEach(items, id: \.0) { group, groupItems in
Section(header: Text(group)) {
ForEach(groupItems, id: \.self) { item in
Text(item)
}
}
}
}
.listStyle(GroupedListStyle())
.navigationTitle("GroupedListStyleの例")
}
}
}
![](https://seeds-digital.com/wp-content/uploads/2023/12/5534b193be60af892d1fdd5bbe793c0b.png)
.listStyle(PlainListStyle())
:
PlainListStyle
は、グループ化されずに単純なリストスタイルを提供します。セクションがなく、全体が一つのリストとして表示されます。
以下の例では、他のリストスタイルとデザインの比較がしやすいようにあえてSectionビューを使っていますが、PlainListStyle
を使う場合、本来は使用しません。
struct ContentView: View {
let items = [
("果物", ["りんご", "バナナ"]),
("野菜", ["にんじん", "キャベツ"]),
]
var body: some View {
NavigationStack {
List {
ForEach(items, id: \.0) { group, groupItems in
Section(header: Text(group)) {
ForEach(groupItems, id: \.self) { item in
Text(item)
}
}
}
}
.listStyle(PlainListStyle())
.navigationTitle("PlainListStyleの例")
}
}
}
![](https://seeds-digital.com/wp-content/uploads/2023/12/195998f710f397fff29425d9c193944b.png)
.listStyle(InsetGroupedListStyle())
:
InsetGroupedListStyle
は、視覚的な観点で言うとGroupedListStyle
に余白が追加されたものです。
struct ContentView: View {
let items = [
("果物", ["りんご", "バナナ"]),
("野菜", ["にんじん", "キャベツ"]),
]
var body: some View {
NavigationStack {
List {
ForEach(items, id: \.0) { group, groupItems in
Section(header: Text(group)) {
ForEach(groupItems, id: \.self) { item in
Text(item)
}
}
}
}
.listStyle(InsetGroupedListStyle())
.navigationTitle("InsetGroupedListStyleの例")
}
}
}
![](https://seeds-digital.com/wp-content/uploads/2023/12/d2f8896a23a11ec3cc3bc27da5a3b535.png)
関連記事
Seeds
![](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
![](https://seeds-digital.com/wp-content/uploads/2023/11/swiftui-list-background.png)
【SwiftUI】Listの背景色を変更する方法 | Seeds
背景色を変更するには scrollContentBackground(_:) を使用します。 .hiddenを設定することで、.backgroundで指定した色が反映されます。 List { // Listの中身 } .scrollC…
Comment