我有一个结构,它从 CoreData 中洗牌和列出记录。我想用按钮重新加载/刷新列表视图。我尝试使用 Button 中的函数。有没有办法做到这一点?
var body: some View {
VStack {
List {
ForEach(dictionary.shuffled().prefix(upTo: 10),id: \.self) { word in
HStack {
Text("\(word.englishWord)")
.foregroundColor(Color.blue)
Text("| \(word.urhoboWord) |")
.foregroundColor(Color.green)
Image(word.imageName)
.resizable()
.frame(width:40, height: 40)
}//HStack
}//End of ForEach
}//End of List
//Button to reload and shuffle list
Button(action: {}) {
Text("Shuffle")
.padding()
.background(Color.black)
.foregroundColor(Color.white)
.cornerRadius(6)
}
.navigationBarTitle(Text("Begin Learning"),displayMode: .inline)
Run Code Online (Sandbox Code Playgroud) 如果图像存在,我正在尝试执行代码。问题是我无法捕获空图像调用的状态。结果是我得到一个空图像,但如果可能的话宁愿放置一个占位符图像。
func procImage(inName: String) {
switch (inName) {
case inName:
imageName = inName.lowercased()
default:
imageName = "blank"
}
}
Run Code Online (Sandbox Code Playgroud) 我是 SwiftUI 的新手,我希望在定位无法居中的自定义按钮方面获得一些帮助。我已经使用 VStack 和 HStack 尝试让按钮底部居中,但按钮保持左对齐。任何帮助将不胜感激。
struct ContentView: View {
var body: some View {
VStack {
NavigationView {
Section {
VStack(alignment: .leading) {
Text("Meal Description").foregroundColor(.green)
Spacer()
NavigationLink(destination: PoundedYam()) {
Text("Pounded Yam & Egusi Soup")
}
NavigationLink(destination: YamPepSoup()) {
Text("Yam and Pepper Soup")
}
NavigationLink(destination: JollofRice()) {
Text("Jollof Rice & Plantain")
}
Spacer()
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity, alignment: .topLeading)
}.padding()
}
Section {
Image("africanfoods")
.resizable()
.frame(width: 275.0, height: 250.0)
.clipShape(Circle())
.overlay(Circle().stroke(Color.black, lineWidth: 5))
.scaledToFit()
} …Run Code Online (Sandbox Code Playgroud)