Post

Replies

Boosts

Views

Activity

Howto releaseData with UITableView prefetching
I develop an app with a UITableView extracting data from a web server. to spare communication between my app and my webserver, I calculate how many rows my tableView can display and I ask the web werver to send me a array containing this number of data. when prefetching,rows, if the ;data corresponding to the indexPath is not in memory, I call the web server, otherwise I extract the data from memory. everything works fine, but I notice that the amount of data grows at each call to the webserver, and is never released. to release some data, I thought using tableview(cancelPrefetchingForRowsAt), but i'm not sure it is the good way to do it. I'm not sure that indexPathes calld in the cancelPrefetching method correspond to rows that UITableView doesn't use
0
0
355
Feb ’23
SwiftUI Modifiers on generic Views
I created a control which group a TextField, a Text as the label of the TextField, and eventually a Text to signal an error The result will be something like that: To declare this control, I write the following Code: struct TextFieldWithError<T: Hashable>: View { var label: String @Binding var text: String @Binding var textError: String var focusId: T? @FocusState.Binding var focus: T? var body: some View { VStack { HStack { if layout == .hstack { Text(label) } if let focusId { TextField(label, text: $text) .focused($focus, equals: focusId) } else { TextField(label, text: $text) .modifier(TextFieldStyle()) } } if textError != "" { Spacer() .frame(height:2) HStack { Text(textError) .foregroundColor(.red) .frame(height: textError == "" ? 0 : 20) .animation(.default) } } } } } if I use a generic for my control, it's because I want to control focus with the .focused modifier and I didn't found another way to do this now, I want to control the width of the label with a modifier. I thought about writing .TextFieldWithErrorFrame(labelWidth: <some CGFloatvalue>) struct TextFieldWithErrorFrame: ViewModifier { varlabelWidth: CGFloat func body(content: Content) -> some View { content } } I think that in place of writing content in the body of my modifier, I have to write the same code that I wrote for the control body, applying a frame modifier on the Label. But I don't find the way of doing this. if I declare the modifier as this: struct TextFieldWithErrorFrame<T>: ViewModifier where T: Hashable { var width: CGFloat func body(content: TextFieldWithError<T>) -> some View { content } } I need to declare an type alias for the body, because I have the error: Type 'TextFieldWithErrorFrame' does not conform to protocol 'ViewModifier' if I use typealias Body = TextFieldWithErrorFrame<T> I have the same error and that's not all. how to rewrite the content in the body of the modifier?
0
0
870
Jun ’23
Swiftui and verticalTraitClass
For my app working on iPhone and IPad, I have two different designs depending on the landscape or portrait orientation. But it's not so simple, because for iPad, the design is the same while in portrait or landscape orientation. So I thought to user verticalSizeClass. If it is compact, I apply portrait design, if it is Regular, I apply landscape design. So I introduce the environment variable: @Environment(\.verticalSizeClass) var verticalSizeClass: UserInterfaceSizeClass? And create the following modifier (found on Apple Developper Forum) struct DeviceRotationViewModifier: ViewModifier { let action: (UIDeviceOrientation) -> Void func body(content: Content) -> some View { content .onAppear() .onReceive(NotificationCenter.default.publisher(for: UIDevice.orientationDidChangeNotification)) { _ in action(UIDevice.current.orientation) } } } // A View wrapper to make the modifier easier to use extension View { func onRotate(perform action: @escaping (UIDeviceOrientation) -> Void) -> some View { self.modifier(DeviceRotationViewModifier(action: action)) } } Implementing the OnRotate modifier on my contentView .onRotate { newOrientation in print(verticalSizeClass) } I find that the verticalSizeClass is not correct on first call of OnRotate (it's always regular). After when I change orientation, the verticalsizeClass becomes correct. What did I missed?
0
0
507
Sep ’23