1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

SwiftUIのListとTextFieldでインクリメンタルサーチっぽいあれ

Posted at

とりあえずサンプルコード
ポイントは空の時のフィルター

元の要件で「選択したものはリストから削除して別途リストを作り、追加削除できるように」とあったので、Setを別に用意して含まれるかどうかも条件に追加した。
5000件レコードを放り込んでも大丈夫

BookView.swift![Something went wrong]()

import SwiftUI

struct BookView: View {
    
    struct Book: Identifiable {
        let id :String = UUID().uuidString
    }
    
    var bookList: [Book] = []
    @State var searchText: String = ""
    @State var selected =  Set<String>()
    
    init() {
        for _ in 0...5001 {
            bookList.append(Book())
        }
    }
    
    var body: some View {
        TextField("search", text: $searchText)
        VStack{
            ForEach(Array($selected.wrappedValue),id:\.self) { book in
                Button {
                    selected.remove(book)
                } label: {
                    Text(book)
                }
            }
        }
        List(bookList.filter { ($searchText.wrappedValue.isEmpty || $0.id.contains($searchText.wrappedValue)) && !selected.contains($0.id) }) { book in
            Text("Book id= \(book.id)")
                .onTapGesture {
                    selected.insert(book.id)
                }
            .id(book.id)
        }
    }
}

Simulator Screen Shot - iPhone 13 mini - 2022-02-17 at 14.09.41.png

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?