当 SwiftUI 中的 List 数据源为空时,如何在视图中心显示文本消息? [英] How to display a text message at the Center of the view when the List datasource is empty in SwiftUI?

查看:40
本文介绍了当 SwiftUI 中的 List 数据源为空时,如何在视图中心显示文本消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

struct LandmarkList: View {
    var body: some View {
        NavigationView {
            List(landmarkData) { landmark in
                LandmarkRow(landmark: landmark)
            }
        }
    }
}

当列表视图(表格视图)为空时,我想在视图的中心显示一条消息.在 SwiftUI 中实现这一目标的最佳方法是什么.检查onAppear"中的数据源计数并设置某种 Bool 值是正确的方法吗?

I want to display a message at the centre of the view when the list view (table view) is empty. What is the best way to achieve this in SwiftUI. Is checking for the data source count in "onAppear" and setting some sort of Bool value the correct approach ?

推荐答案

struct LandmarkList: View {
    var body: some View {
        NavigationView {
            if landmarkData.count == 0 {
              VStack {
                Text("is empty")
              } else {
              List(landmarkData) { landmark in
                 LandmarkRow(landmark: landmark)
              }
           }
        }
    }
}

这篇关于当 SwiftUI 中的 List 数据源为空时,如何在视图中心显示文本消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆