Post

Replies

Boosts

Views

Activity

Conditionally showing List or other views
This...is...nuts. Logically, I understand why it doesn't work. If this was ObjC, I'd probably have it worked out by now. But in SwiftUI, I'm stumped. Here's my code: struct CalendarView: View { @StateObject var selectedDate = SelectedDate() @Environment(\.managedObjectContext) private var viewContext @FetchRequest( sortDescriptors: [NSSortDescriptor(keyPath: \Expense.eName, ascending: true)], animation: .default) private var expenses: FetchedResults<Expense> var body: some View { VStack { CalendarHelper(interval: DateInterval(start: .distantPast, end: .distantFuture), date: selectedDate) ForEach(expenses, id:\.self) { exp in if selectedDate.selectedDate.isInSameDay(as: exp.eDueDate ?? Date()) { List { ExpenseRowItemView(eName: exp.eName ?? "", eCategory: exp.eCategoryName ?? "", eDueDate: exp.eDueDate ?? Date(), eImageName: exp.eCategoryName ?? "", showEdit: false) } } } VStack { GeometryReader { geometry in Image("emptyView") .resizable() .aspectRatio(contentMode: .fit) .frame(width: geometry.size.width, height: geometry.size.height) } Text ("No Data") Spacer() } } } } For the sake of argument, lets just say that I've got a UICalendarView connected to Core Data. Its pulling in and marking events on the calendar correctly. When I select a date with a corresponding decoration, the list item shows correctly under the calendar. Because of the way I'm showing this list, when an item is not selected it should show the Image and Text. And...when the view first loads it does. Obviously, when an item on the calendar is selected and it has a corresponding event, the image is pushed down and the Calendar, list item and empty image/text is shown. I've tried adding a bool to only show the image/text when the selected date doesnt match the event date, but because of views and my lack of understanding of MVVM, its not working. Thanks for your help.
1
0
465
Apr ’23
Picker in SwiftUI Form Selection
The picker in my code should default to no selection. But it always defaults to the first item in the array. import SwiftUI struct ContentView: View { @State private var eName:String = "Dave" @State private var eCategoryName:String = "Default" @State private var pickerITems: [String] = ["Dodge","Ford","Chevy","Toyota"] var body: some View { NavigationView { Form { Section(header: Text("General")) { TextField("Default", text: $eName) Picker("Category", selection: $eCategoryName) { //Text("Select").tag(Optional<String>(nil)) ForEach(pickerITems, id: \.self) { Text($0).tag($0) } } Text("Selected Item is: \(eCategoryName)") } } } } } struct ContentView_Previews: PreviewProvider { static var previews: some View { ContentView() } } code-block I've tried setting the variable eCategoryName to an optional and tagging it with an optional string set to nil, but it still shows the default as the first item in the picker. If I add Text("Default").tag("") the first item isn't selectable but still shows up initially checked. The behavior I'm looking for is nothing (blank) in the picker area, and when tapped the list of array items pops up with nothing selected allowing the user to pick the one they want. This is iOS 16 and from what I'm seeing this behavior is new in 16. Thanks!
1
0
756
Apr ’23
Error: No exact matches in reference to static method 'buildExpression'
Getting the error: "No exact matches in reference to static method 'buildExpression'" in the following code: var categoryNames: [String] = [] ForEach(categories, id: \.self) { category in if let categoryName = category.cCategoryName { categoryNames.append(categoryName) } } Note that categories is a variable pointing to my coredata entity and cCategoryName is an attribute of that entity. I don't have any methods in any of my code called "buildExpression". Is this a bug? Thanks!
3
0
13k
Apr ’23