Post

Replies

Boosts

Views

Activity

UICollectionView self-sizing (list height) cells
I am trying to build a UICollectionView where the rows expand / collapse (for example, where the row only has a title initially, but when tapped expands to show a text view). I'm aware I can use UIContentConfiguration to swap a SwiftUI view for the cell's contentView, but I'm trying to do this in UIKit for configurability. I have watched WWDC 22, 10068: What's new in UIKit, which made this sound easy... I am using UICollectionLayoutListConfiguration for the UICollectionViewLayout. When a cell is tapped, I update the subviews' constraints to the "expanded" state, but this just causes the subviews to expand beyond the cell height. All subviews are on cell.contentView. Calling layoutIfNeeded, invalidateIntrinsicContentSize, on cell doesn't change the cell height. I assumed all I would need to do is set collectionView.selfSizingInvalidation = .enabledIncludingConstraints, but is overriding intrinsicContentSize of the cell the only way to accomplish this?
0
0
141
Oct ’24
Core Data + CloudKit - I need to get FetchRequest's data into my Model
I have been working on this for a while and haven't found a solution. I have a populated public core data database (used readonly in the app) that I need to pull from on launch. This ONLY works with SwiftUI via @FetchRequest or FetchRequest -- NSFetchResultsController, attempting to do it with persistentCloudKitContainer.viewContext.perform {} doesn't fetch (all do work after relaunching the app). The only thing that works is FetchRequest, but I need to do things in my model as soon as I get that data from CloudKit (create local data in response). How can I set, in my model, allItems to the wrappedValue property of the FetchRequest or @FetchRequest ? Since it is from cloudkit just setting the variable won't work, I need to use combine, but I can't wrap my head around how. If anyone has any suggestions -- I'd much appreciate the assistance. class MyViewModel: ObservableObject {     typealias PublisherType = PassthroughSubject<MyViewModel, Never>     let objectWillChange = ObservableObjectPublisher()     let objectDidChange = ObservableObjectPublisher() @Published var allItems: [Item] = [] struct MyView: View {     @FetchRequest var fetchRequest = FetchRequest<Item>(entity: Item.entity(), sortDescriptors: [NSSortDescriptor(key: "name", ascending: true)])     var body: some View {            List(fetchRequest.wrappedValue) { (item:Item) in Text(item.name) } }
1
0
973
Sep ’21
TabView on WatchOS (SwiftUI)
I'm having trouble implementing the TabView component on watchOS - wondering if anyone knows something I'm missing. It works fine on my iOS target, but on watchOS, a TabView with a List lets me tap one item, but returning to the main view and then tapping a second item won't result in the second view being loaded again. It works fine without the tabview of course. class Item: ObservableObject, Identifiable {     let name: String     init(name: String) {         self.name = name     } } struct ContentView: View {     @State var contents: [Item] = [Item(name: "Test"), Item(name: "Test 2")]     var body: some View {         TabView {         List(contents) { (item: Item) in             NavigationLink(item.name) {                 ItemView(item: item)             }         }         }     } } struct ItemView: View {     @State var item: Item     var body: some View {         VStack {             Text(item.name)         }     } }
1
0
1.1k
Aug ’21
Seeding user's core data store with a pre-made core data database
So I have this technique I've found I thought would work to seed a core data db with one I've made. My goal list to take a sqlite database (made with a rough companion app in swiftui), filled with content, that will be part of the application bundle. The goal is to copy that data into the user's core data store and allow them to favorite, make certain changes to the items loaded into the database. Is this technique I'm trying to use best for read-only databases or could I be implementing this wrong? Or is my best bet to copy over the sqlite once with FileManager? This is. my persistent container code: &#9;&#9;lazy var persistentContainer: NSPersistentContainer = { &#9;&#9;&#9;&#9; &#9;&#9;&#9;&#9;let container = NSPersistentContainer(name: "UserStore") &#9;&#9;&#9;&#9; &#9;&#9;&#9;&#9;if isFirstLaunch() { &#9;&#9;&#9;&#9;&#9;&#9; &#9;&#9;&#9;&#9;&#9;&#9;let seededDataURL = Bundle.main.url(forResource: "PopulatedStore", withExtension: "sqlite") &#9;&#9;&#9;&#9;&#9;&#9; &#9;&#9;&#9;&#9;&#9;&#9;if let seededDataURL = seededDataURL { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;let persistentDescription = NSPersistentStoreDescription(url:seededDataURL) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;persistentDescription.shouldInferMappingModelAutomatically = true &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;persistentDescription.shouldMigrateStoreAutomatically = true &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9; &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;container.persistentStoreDescriptions = [persistentDescription] &#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9; &#9;&#9;&#9;&#9;container.loadPersistentStores { (storeDescription, error) in &#9;&#9;&#9;&#9;&#9;&#9;if let error = error { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;print("error loading persistent store! \(error)") &#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;return container &#9;&#9;}() Thank you
2
0
2.2k
Jul ’20