Hello everyone,
I am new to the Swift and the SwiftUI. Trying to understand how sorting works on SwiftUI Table view.
The following code does what I intend to do, but I am not able to understand how does it actually work. Need help with the answers for the questions posted below the code.
import SwiftUI
struct Person: Identifiable {
let givenName: String
let familyName: String
let emailAddress: String
let id = UUID()
}
private var people = [
Person(givenName: "f1", familyName: "l1", emailAddress: "e1@example.com"),
Person(givenName: "f2", familyName: "l2", emailAddress: "e2@example.com"),
Person(givenName: "f3", familyName: "l3", emailAddress: "e3@example.com")
]
struct ContentView: View {
@State private var selected = Set<Person.ID>()
// Question 1
@State private var sortOrder = [KeyPathComparator(\Person.givenName)]
var body: some View {
Table(people, selection: $selected, sortOrder: $sortOrder) {
TableColumn("Given name", value: \.givenName)
TableColumn("Family name", value: \.familyName)
TableColumn("Email", value: \.emailAddress)
}
// Question 2
.onChange(of: sortOrder) { newOrder in
people.sort(using: newOrder)
}
}
}
Question 1 : I want to be able to sort by all the columns. How many KeyPathComparators should be in the sortOrder array?
Question 2: What is the value of newOrder? Is it same as the sortOrder? What does it contain?
Question 3: sortOrder contains the comparator for only givenName. How does the above code able to provide sort functionality for all the columns?
Selecting any option will automatically load the page
Post
Replies
Boosts
Views
Activity
Hello everyone,
I am new to the app development. My goal is to develop professional apps for the MacOS.
I have learned some Swift and SwiftUI on my own. Also, watched some videos here and there. Apple tutorials are also scattered. I feel I know some things, but not enough to create the apps that I want. A lot of my time goes into searching things, which kills my interest.
Is there a learning path (things to learn in sequence) for MacOS app development? Example:
learn swift
learn swiftUI
design patterns?
some frameworks, maybe?
Please suggest.
Thanks,
Hi
I want to contribute to a large open source project written in C language. The project uses Makefile for building.
How can I import this project into Xcode? I don't see any "import" option in Xcode 14.
Also, please suggest if Xcode the right tool for a large project written in C?
Hello,
I am new to the Swift and the SwiftUI. I want to build a MacOS app for learning purpose. Looks like I am stuck, so looking for some help.
The code below does not execute the print statement. Why is the timer not working? What am I missing? Thanks in advance.
struct CustomView: View {
let timer = Timer.publish(every: 1, on: .current, in: .common).autoconnect()
private var nums: [String] = ["one", "two"]
var body: some View {
ForEach(nums, id: \.self) {num in
Text(num)
}
.onReceive(timer) { time in
// why is this not working?
print("time is \(time)")
}
}
}
@main struct Writer: App {
var body: some Scene {
MenuBarExtra("Writer util", systemImage: "pencil.line") {
CustomView()
}
.menuBarExtraStyle(.menu)
}
}
Hi
I have an existing apple id (phone number based). How do I create a developer account using it?
Thanks in advance
In the following code, click/touch anywhere on any row of the list deletes that row. This is not expected. Only the click/touch on the "xmark" button should delete that row.
import SwiftUI
struct MainView: View {
private let arr: [String] = ["one", "two", "three"]
var body: some View {
List(arr, id: \.self) {item in
SecView(name: item)
}
}
}
struct SecView: View {
var name: String
@State private var show = true
var body: some View {
if show {
HStack {
Text(name)
Button(action: {
show = false
}, label: {
Image(systemName: "xmark")
})
.border(.yellow)
}
}
}
}
@main
struct XApp: App {
var body: some Scene {
WindowGroup {
MainView()
}
}
}
Please check. I ran the above code for iPAD air (5th generation).
Hello and thanks for reading my post.
I have a SwiftUI view, the users should be able to click a button and take printout of that view. Clicking on the button should open the standard print sheet (select printer, pages, layout, etc.).
How can I implement such a functionality? I have been trying hard without any success. Please help.
It is an iPad app, using Xcode 14.3
Hello and thanks for reading my post.
I have been trying hard to generate a PDF from a SwiftUI view on a button press.
Looked at the PDFKit documentation, understood that PDFView, PDFDocument and PDFPage are important classes. However, I couldn't find any examples of how to use them in SwiftUI or Swift.
Basically, given a SwiftUI view (or a Swift struct), how to create a new SwiftUI view that displays the generated PDF? The pdf can contain charts, layouts, etc.