前提・実現したいこと
swiftUIにてfetchRequestにてTabviewの内容を正確に表示させたいです。
ここに質問の内容を詳しく書いてください。
https://www.hackingwithswift.com/books/ios-swiftui/dynamically-filtering-fetchrequest-with-swiftui
上記2サイトを参考にした所、List表示では問題なく動作しますが
Tabview表示にすると正常動作しません。
(Tabviewの横スライドが最後までできません)
発生している問題・エラーメッセージ
特にエラーメッセージは発生していませんが、挙動がおかしいです。
ForEach文のMAX値が必要なのかもしれませんが、どのように適用すればよいのか、わかりません。
ご指導、よろしくお願いします。
import SwiftUI import CoreData struct ContentView: View { @Environment(.managedObjectContext) var moc @State private var lastNameFilter = "A" var body: some View { VStack{ FilteredList(filter: lastNameFilter) Button("Add Examples") { let taylor = Singer(context: self.moc) taylor.firstName = "Taylor" taylor.lastName = "Swift" let ed = Singer(context: self.moc) ed.firstName = "Ed" ed.lastName = "Sheeran" let adele = Singer(context: self.moc) adele.firstName = "Adele" adele.lastName = "Adkins" try? self.moc.save() } Button("Show A") { self.lastNameFilter = "A" } Button("Show S") { self.lastNameFilter = "S" } } } } struct FilteredList: View { var fetchRequest : FetchRequest<Singer> @Environment(.managedObjectContext) var context var result: FetchedResults<Singer> { fetchRequest.wrappedValue } var body: some View { TabView{ //List { ForEach(fetchRequest.wrappedValue, id: .self) { singer in Text("(singer.wrappedFirstName)(singer.wrappedLastName) ") }.onDelete(perform: deleteitems) } .tabViewStyle(PageTabViewStyle()) .indexViewStyle(PageIndexViewStyle(backgroundDisplayMode: .always)) .background(Color .orange) } init(filter: String) { fetchRequest = FetchRequest<Singer>(entity: Singer.entity(), sortDescriptors: [], predicate: NSPredicate(format: "lastName BEGINSWITH %@", filter)) } private func deleteitems(offsets: IndexSet){ offsets.map { fetchRequest.wrappedValue[$0] }.forEach(context.delete) try! context.save() } } struct ContentView_Previews: PreviewProvider { static var previews: some View { ContentView() } } import SwiftUI import CoreData struct ContentView: View { @Environment(.managedObjectContext) var moc @State private var lastNameFilter = "A" var body: some View { VStack{ FilteredList(filter: lastNameFilter) Button("Add Examples") { let taylor = Singer(context: self.moc) taylor.firstName = "Taylor" taylor.lastName = "Swift" let ed = Singer(context: self.moc) ed.firstName = "Ed" ed.lastName = "Sheeran" let adele = Singer(context: self.moc) adele.firstName = "Adele" adele.lastName = "Adkins" try? self.moc.save() } Button("Show A") { self.lastNameFilter = "A" } Button("Show S") { self.lastNameFilter = "S" } } } } struct FilteredList: View { var fetchRequest : FetchRequest<Singer> @Environment(.managedObjectContext) var context var result: FetchedResults<Singer> { fetchRequest.wrappedValue } var body: some View { TabView{ //List { ForEach(fetchRequest.wrappedValue, id: .self) { singer in Text("(singer.wrappedFirstName)(singer.wrappedLastName) ") }.onDelete(perform: deleteitems) } .tabViewStyle(PageTabViewStyle()) .indexViewStyle(PageIndexViewStyle(backgroundDisplayMode: .always)) .background(Color .orange) } init(filter: String) { fetchRequest = FetchRequest<Singer>(entity: Singer.entity(), sortDescriptors: [], predicate: NSPredicate(format: "lastName BEGINSWITH %@", filter)) } private func deleteitems(offsets: IndexSet){ offsets.map { fetchRequest.wrappedValue[$0] }.forEach(context.delete) try! context.save() } } struct ContentView_Previews: PreviewProvider { static var previews: some View { ContentView() } }
import Foundation import CoreData extension Singer { @nonobjc public class func fetchRequest() -> NSFetchRequest<Singer> { return NSFetchRequest<Singer>(entityName: "Singer") } @NSManaged public var firstName: String? @NSManaged public var lastName: String? var wrappedFirstName: String { firstName ?? "Unknown" } var wrappedLastName: String { lastName ?? "Unknown" } } extension Singer : Identifiable { }
import Foundation import CoreData @objc(Singer) public class Singer: NSManagedObject { }
試したこと
AppleフォーラムやStack Overflowを調べましたが
Tabviewのバグ?という意見もあります。
補足情報(FW/ツールのバージョンなど)
XCODE Version 12.1
IOS14.1
ここにより詳細な情報を記載してください。
あなたの回答
tips
プレビュー