Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save karthikAdaptavant/8033574b95e5854100839e6acacc2632 to your computer and use it in GitHub Desktop.
Save karthikAdaptavant/8033574b95e5854100839e6acacc2632 to your computer and use it in GitHub Desktop.
FetchedResultsTableViewControllerWrapper
public struct FetchedResultsTableViewControllerWrapper<Entity: NSFetchRequestResult, Content: View>: UIViewControllerRepresentable {
public typealias UIViewControllerType = FetchedResultsTableViewController<Entity, Content>
/// SwiftUI
@Binding var searchText: String?
let cellContent: (Entity, _ isSelected: Bool) -> Content
public init(
searchText: Binding<String?>,
cellContent: @escaping (Entity, _ isSelected: Bool) -> Content
) {
self._searchText = searchText
self.cellContent = cellContent
}
public func makeUIViewController(context: Context) -> FetchedResultsTableViewController<Entity, Content> {
// NOTE: Pass your UITableViewController that Holds NSFetchedResultscontroller Here.
let viewController = FetchedResultsTableViewController(parentView: self, context: self.context)
return viewController
}
public func updateUIViewController(_ uiViewController: FetchedResultsTableViewController<Entity, Content>, context: Context) {
/// NOTE: Updating tableview when query changes.
uiViewController.update(with: searchText)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment