SwiftUI 基礎(chǔ)之05 list 和 searchbar (2020)
首要
struct ContentView: View {
let names = ["Raju", "Ghanshyam", "Baburao Ganpatrao Apte", "Anuradha", "Kabira", "Chaman Jhinga", "Devi Prasad", "Khadak Singh"]
@State private var searchTerm : String = ""
var body: some View {
NavigationView{
List {
SearchBar(text: $searchTerm)
ForEach(self.names.filter{
self.searchTerm.isEmpty ? true : $0.localizedStandardContains(self.searchTerm)
}, id: \.self) { name in
Text(name)
}
}
.navigationBarTitle(Text("Search Bar"))
}
}
}
searchbar
import SwiftUI
struct SearchBar : UIViewRepresentable {
@Binding var text : String
class Cordinator : NSObject, UISearchBarDelegate {
@Binding var text : String
init(text : Binding<String>) {
_text = text
}
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
text = searchText
}
}
func makeCoordinator() -> SearchBar.Cordinator {
return Cordinator(text: $text)
}
func makeUIView(context: UIViewRepresentableContext<SearchBar>) -> UISearchBar {
let searchBar = UISearchBar(frame: .zero)
searchBar.delegate = context.coordinator
return searchBar
}
func updateUIView(_ uiView: UISearchBar, context: UIViewRepresentableContext<SearchBar>) {
uiView.text = text
}
}
效果圖
推薦文章
CoreData篇
- SwiftUI數(shù)據(jù)存儲(chǔ)之做個(gè)筆記App 新增與查詢(CoreData)
- SwiftUI進(jìn)階之存儲(chǔ)用戶狀態(tài)實(shí)現(xiàn)登錄與登出
- SwiftUI 數(shù)據(jù)之List顯示Sqlite數(shù)據(jù)庫內(nèi)容(2020年教程)
TextField篇
- 《SwiftUI 一篇文章全面掌握TextField文本框 (教程和全部源碼)》
- 《SwiftUI實(shí)戰(zhàn)之TextField風(fēng)格自定義與formatters》
- 《SwiftUI實(shí)戰(zhàn)之TextField如何給鍵盤增加個(gè)返回按鈕(隱藏鍵盤)》
- 《SwiftUI 當(dāng)鍵盤出現(xiàn)時(shí)避免TextField被遮擋自動(dòng)向上移動(dòng)》
- 《SwiftUI實(shí)戰(zhàn)之TextField如何給鍵盤增加個(gè)返回按鈕(隱藏鍵盤)》
JSON文件篇
技術(shù)交流
QQ:3365059189
SwiftUI技術(shù)交流QQ群:518696470
請(qǐng)關(guān)注我的專欄icloudend,
http://www.reibang.com/c/7b3e3b671970