如果列表视图为空,则显示文本 "Your list is empty" 的 if 语句
An if statement that shows text "Your list is empty" if the List view is empty
我是初学者 iOS 使用 swiftUI 的开发人员,如果 TaskView 中的列表为空,我想创建一个显示文本“您的列表为空”的 if 语句。我应该在 if else 语句中放入什么?这是我的代码:
import SwiftUI
struct TasksView: View {
@EnvironmentObject var RealmManager: RealmManager
var body: some View {
VStack {
Text("My List")
.font(.title3).bold()
.foregroundColor(.black)
.padding()
.frame(maxWidth: .infinity, alignment: .leading)
List {
ForEach(RealmManager.tasks, id: \.id) {
task in
if !task.isInvalidated {
TaskRow(task: task.title, completed: task.completed)
.onTapGesture {
RealmManager.updateTask(id: task.id, completed: !task.completed)
}
.swipeActions(edge: .trailing) {
Button(role: .destructive) {
RealmManager.deleteTask(id: task.id)
enter image description here
您可以在视图中简单地使用 if
语句,如下所示:
Text("My List")
.font(.title3).bold()
.foregroundColor(.black)
.padding()
.frame(maxWidth: .infinity, alignment: .leading)
// Here is the condition:
if RealmManager.tasks.isEmpty {
Text("Your list is empty")
} else {
List {
ForEach(RealmManager.tasks, id: \.id) {
task in
if !task.isInvalidated {
TaskRow(task: task.title, completed: task.completed)
.onTapGesture {
RealmManager.updateTask(id: task.id, completed: !task.completed)
}
使用三元条件运算符的简单解决方案
Text("My List\(realmManager.tasks.isEmpty ? " is empty" : "")")
请以小写字母开头的变量命名,var RealmManager: RealmManager
非常混乱
我是初学者 iOS 使用 swiftUI 的开发人员,如果 TaskView 中的列表为空,我想创建一个显示文本“您的列表为空”的 if 语句。我应该在 if else 语句中放入什么?这是我的代码:
import SwiftUI
struct TasksView: View {
@EnvironmentObject var RealmManager: RealmManager
var body: some View {
VStack {
Text("My List")
.font(.title3).bold()
.foregroundColor(.black)
.padding()
.frame(maxWidth: .infinity, alignment: .leading)
List {
ForEach(RealmManager.tasks, id: \.id) {
task in
if !task.isInvalidated {
TaskRow(task: task.title, completed: task.completed)
.onTapGesture {
RealmManager.updateTask(id: task.id, completed: !task.completed)
}
.swipeActions(edge: .trailing) {
Button(role: .destructive) {
RealmManager.deleteTask(id: task.id)
enter image description here
您可以在视图中简单地使用 if
语句,如下所示:
Text("My List")
.font(.title3).bold()
.foregroundColor(.black)
.padding()
.frame(maxWidth: .infinity, alignment: .leading)
// Here is the condition:
if RealmManager.tasks.isEmpty {
Text("Your list is empty")
} else {
List {
ForEach(RealmManager.tasks, id: \.id) {
task in
if !task.isInvalidated {
TaskRow(task: task.title, completed: task.completed)
.onTapGesture {
RealmManager.updateTask(id: task.id, completed: !task.completed)
}
使用三元条件运算符的简单解决方案
Text("My List\(realmManager.tasks.isEmpty ? " is empty" : "")")
请以小写字母开头的变量命名,var RealmManager: RealmManager
非常混乱