swiftui - 来自 Data SwiftUI 的基于列表的视图
问题描述
有错误的新代码。这就是我得到的错误。我有 2 组数据,将分 2 组显示。位置 1 有员工,位置 2 有不同的员工。当我添加第二个列表 if 员工时,我在 Loc2 文件Cannot convert value of type 'DataUI' to expected argument type 'Data
的行上得到错误。NavigationLink(destination: DetailView(data: listedPeople)) {
希望这能很好地解释它。
放入 1 个名为ContentView
struct Home: View {
var body: some View {
TabView {
loc1()
.tabItem {
VStack{
Image(systemName: "person.3.fill")
Text ("Location 1")
}
}
.tag(2)
loc2()
.tabItem {
VStack{
Image(systemName: "person.fill")
Text ("Location 2")
}
}
}
}
}
放入文件 2 命名loc1
struct Data: Identifiable{
var id = Int ()
let title, imageUrl, Dev, URL: String
}
struct loc1: View {
let data:[Data] = [
Data(id: 0, title: "Cook", imageUrl: "hh",Dev:"John", URL: "school"),
Data(id: 1, title: "Staff", imageUrl: "JJ",Dev:"Harper", URL: "home" ),
Data(id: 2, title: "Busser", imageUrl: "uu",Dev:"Matt", URL: "Table"),
Data(id: 3, title: "Host", imageUrl: "tt",Dev:"Jacob", URL: "Door")]
var body: some View {
NavigationView {
List(data) { listedPeople in
NavigationLink(destination: DetailView(data: listedPeople)) {
HStack{
Image(listedPeople.imageUrl)
.resizable()
.cornerRadius(12)
.frame(width:30, height:30)
VStack (alignment: .leading){
Text(listedPeople.title)
.font(.headline)
Text(listedPeople.Dev)
.font(.subheadline)
}
}
}.navigationBarTitle(Text("location 1"))
}
}
}
}
将代码放在名为的新文件中loc2
(这是我的错误所在)
import SwiftUI
struct DataUI: Identifiable{
var id = Int ()
let title, imageUrl, Dev, URL: String
}
struct loc2: View {
let data:[DataUI] = [
DataUI(id: 0, title: "Cook", imageUrl: "hh",Dev:"Bob", URL: "school"),
DataUI(id: 1, title: "Staff", imageUrl: "JJ",Dev:"Joe", URL: "home" ),
DataUI(id: 2, title: "Busser", imageUrl: "uu",Dev:"Nick", URL: "Table"),
DataUI(id: 3, title: "Host", imageUrl: "tt",Dev:"Hunter", URL: "Door")]
var body: some View {
NavigationView {
List(data) { listedPeople in
NavigationLink(destination: DetailView(data: listedPeople)) {
HStack{
Image(listedPeople.imageUrl)
.resizable()
.cornerRadius(12)
.frame(width:30, height:30)
VStack (alignment: .leading){
Text(listedPeople.title)
.font(.headline)
Text(listedPeople.Dev)
.font(.subheadline)
}
}
}.navigationBarTitle(Text("Location2"))
}
}
}
}
创建一个名为的新文件DetailView
import SwiftUI
struct DetailView : View{
var data: Data
var body: some View {
NavigationView{
List {
HStack{
Image(data.imageUrl)
.resizable()
.frame(width:70, height:60)
.clipShape(Circle())
.shadow(radius: 10)
.overlay(Circle().stroke(Color.black, lineWidth: 1))
VStack{
Text(data.title)
.font (.title)
HStack{
Image(systemName: "envelope.fill")
.resizable()
.frame(width:20, height: 15)
Text("Data.URL")
.font (.subheadline)
}
}
}.navigationBarTitle(Text("Data.title"))
}
}
}
}
解决方案
您混合了 Data 和 DataUI ...这就是问题所在。我真的不知道为什么你定义了两个不同名称的结构,它们完全一样?
我纠正了这个,现在它运行了,
这是解决方案:
import SwiftUI
struct ContentView: View {
var body: some View {
TabView {
loc1()
.tabItem {
VStack{
Image(systemName: "person.3.fill")
Text ("Location 1")
}
}
.tag(2)
loc2()
.tabItem {
VStack{
Image(systemName: "person.fill")
Text ("Location 2")
}
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
struct loc1: View {
let dataUI:[DataUI] = [
DataUI(id: 0, title: "Cook", imageUrl: "hh",Dev:"John", URL: "school"),
DataUI(id: 1, title: "Staff", imageUrl: "JJ",Dev:"Harper", URL: "home" ),
DataUI(id: 2, title: "Busser", imageUrl: "uu",Dev:"Matt", URL: "Table"),
DataUI(id: 3, title: "Host", imageUrl: "tt",Dev:"Jacob", URL: "Door")]
var body: some View {
NavigationView {
List(dataUI) { listedPeople in
NavigationLink(destination: DetailView(dataUI: listedPeople)) {
HStack{
Image(listedPeople.imageUrl)
.resizable()
.cornerRadius(12)
.frame(width:30, height:30)
VStack (alignment: .leading){
Text(listedPeople.title)
.font(.headline)
Text(listedPeople.Dev)
.font(.subheadline)
}
}
}.navigationBarTitle(Text("location 1"))
}
}
}
}
struct loc1_Previews: PreviewProvider {
static var previews: some View {
loc1()
}
}
struct DataUI: Identifiable{
var id = Int ()
let title, imageUrl, Dev, URL: String
}
struct loc2: View {
let dataUI:[DataUI] = [
DataUI(id: 0, title: "Cook", imageUrl: "hh",Dev:"Bob", URL: "school"),
DataUI(id: 1, title: "Staff", imageUrl: "JJ",Dev:"Joe", URL: "home" ),
DataUI(id: 2, title: "Busser", imageUrl: "uu",Dev:"Nick", URL: "Table"),
DataUI(id: 3, title: "Host", imageUrl: "tt",Dev:"Hunter", URL: "Door")]
var body: some View {
NavigationView {
List(dataUI) { listedPeople in
NavigationLink(destination: DetailView(dataUI: listedPeople)) {
HStack{
Image(listedPeople.imageUrl)
.resizable()
.cornerRadius(12)
.frame(width:30, height:30)
VStack (alignment: .leading){
Text(listedPeople.title)
.font(.headline)
Text(listedPeople.Dev)
.font(.subheadline)
}
}
}.navigationBarTitle(Text("Location2"))
}
}
}
}
struct DetailView : View{
var dataUI: DataUI
var body: some View {
NavigationView{
List {
HStack{
Image(dataUI.imageUrl)
.resizable()
.frame(width:70, height:60)
.clipShape(Circle())
.shadow(radius: 10)
.overlay(Circle().stroke(Color.black, lineWidth: 1))
VStack{
Text(dataUI.title)
.font (.title)
HStack{
Image(systemName: "envelope.fill")
.resizable()
.frame(width:20, height: 15)
Text("Data.URL")
.font (.subheadline)
}
}
}.navigationBarTitle(Text("Data.title"))
}
}
}
}
推荐阅读
- sql - 如何将 SELECT 使用 Oracle 中 SQL 中的两个表的列中的总和值相加
- tsql - 如何制作一个用户可以输入年份和月份的功能,自动查找该月的最后一天?
- azure - 如何将 WAR 文件部署到使用 Docker OPEN Liberty Image 创建的 Azure 应用服务
- java - cpu使用率高和内存泄漏
- jscript - 有时不支持 Exists 属性
- c# - 选择对象集合的列/字段并导出为 List<>
- reactjs - 将全局变量与局部变量进行比较后如何重定向到另一条路由
- javascript - 从他们的 API 检索 Stripe 发票时,如何扩展对象以检索信用卡详细信息和其他信息?
- quill - How to get the last index of quill editor?
- java - 为什么 JTextfield 和 JButton 只在启动程序 3 次后才出现?