There seems to be no way to add dynamically add table columns to SwfitUI table. I have a column of dates that I store in an array like so.
let dates = [Date]
let values = [Int]
I want one able enumerate over the dates to generate columns. However I can't seem to find a way to enumerate over them and generate my columns. My current data set has 250 columns and it will keep growing.
I would love to do
ForEach(0 ..< dates.length) { index in
TableColumn("<some formatted date>") {
Text("\(value)")
}
}
But it appears that since TableColumn is not a view ForEach can't be used. Is there a supported way to build columns dynamically? This is a Mac application.
Does charts support a for each type symantic too?
Post
Replies
Boosts
Views
Activity
I was playing around with TableColumn api in MacOS Ventura I have the following data model.
actor HousingData: Identifiable {
let id: Int
let rank: Int
let region: String
let regionType: RegionType
let state: String
}
However when it try to do
TableColumn("foo", value: \.rank)
I get
Key path value type 'Int' cannot be converted to contextual type 'String'
if I try
TableColumn("Rank", value: \.rank) { rank in
Text("\(rank)")
}
I get
Referencing initializer 'init(_:value:content:)' on 'TableColumn' requires the types 'Never' and 'KeyPathComparator<HousingData>' be equivalent
If i use a string value all works well.
If I past the samele code
import RegexBuilder
let fieldSeparator = /\s{2,}|\t/
let transactionMatcher = Regex {
/CREDIT|DEBIT/
fieldSeparator
One(.date(.numeric, locale: Locale(identifier: "en_US"), timeZone: .gmt))
fieldSeparator
OneOrMore {
NegativeLookahead { fieldSeparator }
CharacterClass.any
}
fieldSeparator
One(.localizedCurrency(code: "USD").locale(Locale(identifier: "en_US")))
}
It does not like the OneOrMore with the negative look ahead. Is there an updated example of this code from WWDC?
I have a scroll view with one item in it. When is scroll it under a the navigation bar it disappears before the whole cell is gone it seems to be about when ½ the cell is disappeared. Simple LazyVStack
NavigationView {
ZStack {
ScrollView {
LazyVStack {
ForEach(model.wallets, id: \.id) { wallet in
GalleryCell(wallet: wallet)
}
}
}
}
I have a SwiftUI application with a SignInWithAppleButton. However when I press the button with the remote I don't get any callbacks in the blocks. There is no error in the console that I can find.
Should the button work on tvOS 14?
The code is simple:
import SwiftUI
import AuthenticationServices
struct SignInView: View {
var body: some View {
VStack(spacing: 40) {
Text("Welcome to My App")
.font(.system(size: 50.0, weight: .bold))
SignInWithAppleButton(.signIn) { request in
request.requestedScopes = [.fullName, .email]
} onCompletion: { result in
print(result)
}
.signInWithAppleButtonStyle(.whiteOutline)
.frame(width: 300, height: 50)
}
}
}
I am noticing that adjustsImageWhenAncestorFocused causes the image to blur. Is there any way to get this not to happen? I am using vector images. I am trying to create a round button.
I have a really simple app where I am trying to scroll full page between images. However the if I use a LazyHStack the scrolling stops working if a child item is full screen. LazyVStack has the same issue if the content is full screen then scrolling stops working. Normal stacks work but that defeats the purpose since all items have to load. Oh this need to scroll horizontally.
Code is so:
struct ContentView: View {
var body: some View {
GeometryReader { geometry in
ScrollView(.horizontal) {
LazyHStack {
ForEach(1...50, id: \.self) { index in
Button(action: {}){ Text("\(index)")}
.frame(width: geometry.size.width, height: geometry.size.height)
}
}
}
}
.ignoresSafeArea()
}
}
I have view model that keeps state around like so:
class FeedModel: ObservableObject {
enum CurrenState: Equatable {
case none
case loading
case error
case done
}
private (set) var feed: RssFeed?
@Published private (set) var currentState: CurrenState = .none
I did this to keep the model's loading state out of the view logic.
The idea is to disable the dialog and overlay a spinner. Like so:
ZStack {
VStack(alignment:.leading) {
Text("Enter a feed address")
TextField("", text: $text)
.frame(idealWidth: 300)
HStack {
Spacer()
Button("Cancel") {
isOpen = false
}
.keyboardShortcut(.cancelAction)
Button("Add") {
model.load(address: text)
}
.keyboardShortcut(.defaultAction)
}
}
.padding()
.disabled(model.currentState == .loading)
if model.currentState == .loading {
ProgressView()
.progressViewStyle(CircularProgressViewStyle())
}
}
The disable causes a cycle detected message. I actually get several of them. I don't want to move this code out of the model the model is managing some loading and knows what state it is in. If there is more to the loading then I want to be able to have the model mange the state.
The other idea is for this model to be reusable
I am working with Xcode 12 beta 6 and MacOS 11 beta 5 and I have not found a way to lock the size of a sheet.
I have given it max, ideal and min sizes. I have tried to use fixedSize on the sheet and the children. Nothing seems to work.
This feels like a SwiftUI bug on MacOS. Which is beginning to feel like it is not ready for prime time on Mac.
I have the following code written using SwiftUI 2.0, Xcode 12.0 beta 5 on OS 11 beta 5.
struct ContentView: View {
var body: some View {
GeometryReader { geometry in
List {
ForEach(data, id: \.id) { article in
ArticleCell(article: article)
}
.frame(width: geometry.size.width)
.listRowInsets(.init(top: 0, leading: 0, bottom: 0, trailing: 0))
}
.listStyle(PlainListStyle())
}
}
}
struct Article: Identifiable {
let id: String
let title: String
let description: String
}
let data = [
Article(
id: UUID().uuidString,
title: "Title 1",
description: "NASA has awarded the Strategic Research and Analysis, Communications, and Exhibits Services (SRACES) contract to Media Fusion LLC of Huntsville, Alabama, to provide comprehensive strategic research and analysis, communications, and exhibits services at the agency’s Marshall Space Flight Center in Huntsville, Alabama."
),
Article(
id: UUID().uuidString,
title: "Title 2",
description: "Girl Scouts from across the nation will pose questions next week to NASA astronaut Chris Cassidy aboard the International Space Station."
)
];
struct ArticleCell: View {
let article: Article
var body: some View {
VStack(alignment: .leading, spacing: 8) {
Text(article.title)
.font(.title)
Text(article.description)
.font(.body)
.lineLimit(3)
.multilineTextAlignment(.leading)
.fixedSize(horizontal: false, vertical: true)
}
}
}
The issues are the the following:
The article views are sized beyond the size of the cell and are clipping.
The padding fluctuates in the text controls
There is no separator in the cell (at least in dark mode)
Cells are leading aligned like they are told in the VStack.
It appears that swift ui lists are broken on the Mac for complicated lists. I can't seem to do the following:
Remove the spacing between lists using either default or plain styles. I always seem to get what looks like separator but it is near drawn?
Can't remove the padding without hard coding values. Insets seem to be broken.
The default highlight for self sizing cells seem to not highlight properly.
To get around this I have moved to a LazyVStack but then you lose edit functionality of the list.
I can write a simple sample later. Any one had any luck with lists on the Mac beyond a simple text item?
I am tryign to write something that acts like a drod down menu. The view is composed of a VStack with an TextField and List. When the binding has 0 objects i am trying to animate the list into view. The code looks like so:struct Search: Identifiable {
let id: UUID
let text: String
}
struct SearchBox: View {
@State var searchParam: String = ""
@State var stuff = [Search]()
init() {
// To remove only extra separators below the list:
UITableView.appearance().tableFooterView = UIView()
// To remove all separators including the actual ones:
UITableView.appearance().separatorStyle = .none
}
var body: some View {
var binding = Binding(
get: {
self.searchParam
},
set: {
self.stuff.append(
Search(id: UUID(), text: $0))
self.searchParam = $0
})
return VStack(spacing: 0.0) {
TextField("Search", text: binding )
.font(.title)
.padding()
.background(Color.white)
Color(.darkGray)
.frame(height: 1.0)
if stuff.count > 0 {
List(stuff, id: \.id) {
Text($0.text)
}
.transition(.slide)
}
}
}
struct SearchBox_Preview: PreviewProvider {
static var previews: some View{
SearchBox()
}
}
}Its embedded in a ZStack on the content view like so:struct Search: Identifiable {
let id: UUID
let text: String
}
struct SearchBox: View {
@State var searchParam: String = ""
@State var stuff = [Search]()
init() {
// To remove only extra separators below the list:
UITableView.appearance().tableFooterView = UIView()
// To remove all separators including the actual ones:
UITableView.appearance().separatorStyle = .none
}
var body: some View {
var binding = Binding(
get: {
self.searchParam
},
set: {
self.stuff.append(
Search(id: UUID(), text: $0))
self.searchParam = $0
})
return VStack(spacing: 0.0) {
TextField("Search", text: binding )
.font(.title)
.padding()
.background(Color.white)
Color(.darkGray)
.frame(height: 1.0)
if stuff.count > 0 {
List(stuff, id: \.id) {
Text($0.text)
}
.transition(.slide)
}
}
}
struct SearchBox_Preview: PreviewProvider {
static var previews: some View{
SearchBox()
}
}
}I can slow the default fade and i can scale (but not from the top down). But none of the slide or move transitions are working.
It seems that the examples from WWDC on handling commands by extending command doesn't work since Command no longer exists and the onCommand fucntion wants a selector. It also seems that my custom view can't be made focusable. i have the follwong code.struct ContentView: View {
var body: some View {
MyView()
.focusable() { print("\($0)") }
.onCommand(Selector("onAny:")) {
print("foo")
}
}
}
struct MyView: View {
var body: some View {
Rectangle()
.fill(Color.blue)
.frame(width: 400, height: 400, alignment: .topLeading)
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}Is there a proper supported way to do this. Or is SwiftUI not ready for real mac app developement yet? I can't add support for copy or paste yet it seems that the view won't become a first responder.