admin管理员组文章数量:1391766
New to xcode and ios programming
I am trying to create an image grid view of a bunch of images - i need to download the list of filenames/urls and into an array, then loop through the data in the array to display them.
right now i have asyncimage pulling a random image for testing - this works as intended. i need to replace the URL being fetched with data from the array - you will see ProjectInfo defined at the bottom
I can't work out how to replace the ForEach loop with data from the array. right now it is just looping from 1 to 100 and loading a random image. i need to loop through the ProjectInfo struct and download the filename for each record
below is my code:
import SwiftUI
struct ProjectViewPhotos: View
{
@State private var data: [ProjectInfo] = []
let jobid: String
var body: some View
{
ScrollView {
LazyVGrid(columns: [.init(.adaptive(minimum: 100, maximum: .infinity), spacing: 3)], spacing: 3) {
ForEach(1..<100)
{
ix in
VStack(alignment: .leading){
NavigationLink(
destination: ViewImage(project_job_id: "126", image_filename: "pic05.jpg"))
{
AsyncImage(url: URL(string: "/200/300")!)
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
.clipped()
.aspectRatio(1, contentMode: .fit)
}
}.navigationTitle("Project Photos - " + jobid)
}
}
}
}
func fetchData()
{
guard let url = URL(string: ".php?projectid=" + jobid) else { return }
URLSession.shared.dataTask(with: url) { data, response, error in
guard let data = data else { return }
do {
let projectinfo = try JSONDecoder().decode([ProjectInfo].self, from: data)
DispatchQueue.main.async {
self.data = projectinfo
}
} catch {
print(error)
}
}.resume()
}
}
struct ProjectInfo: Codable {
let id: String
let image_filename: String
let project_id: String
}
#Preview {
ProjectViewPhotos(jobid: "47")
}
tried the above which works. when i attempt to use the array i get all sorts of errors depending on what i've tried.
New to xcode and ios programming
I am trying to create an image grid view of a bunch of images - i need to download the list of filenames/urls and into an array, then loop through the data in the array to display them.
right now i have asyncimage pulling a random image for testing - this works as intended. i need to replace the URL being fetched with data from the array - you will see ProjectInfo defined at the bottom
I can't work out how to replace the ForEach loop with data from the array. right now it is just looping from 1 to 100 and loading a random image. i need to loop through the ProjectInfo struct and download the filename for each record
below is my code:
import SwiftUI
struct ProjectViewPhotos: View
{
@State private var data: [ProjectInfo] = []
let jobid: String
var body: some View
{
ScrollView {
LazyVGrid(columns: [.init(.adaptive(minimum: 100, maximum: .infinity), spacing: 3)], spacing: 3) {
ForEach(1..<100)
{
ix in
VStack(alignment: .leading){
NavigationLink(
destination: ViewImage(project_job_id: "126", image_filename: "pic05.jpg"))
{
AsyncImage(url: URL(string: "https://picsum.photos/200/300")!)
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
.clipped()
.aspectRatio(1, contentMode: .fit)
}
}.navigationTitle("Project Photos - " + jobid)
}
}
}
}
func fetchData()
{
guard let url = URL(string: "https://mywebsite/index.php?projectid=" + jobid) else { return }
URLSession.shared.dataTask(with: url) { data, response, error in
guard let data = data else { return }
do {
let projectinfo = try JSONDecoder().decode([ProjectInfo].self, from: data)
DispatchQueue.main.async {
self.data = projectinfo
}
} catch {
print(error)
}
}.resume()
}
}
struct ProjectInfo: Codable {
let id: String
let image_filename: String
let project_id: String
}
#Preview {
ProjectViewPhotos(jobid: "47")
}
tried the above which works. when i attempt to use the array i get all sorts of errors depending on what i've tried.
Share Improve this question edited Mar 12 at 8:50 Joakim Danielson 52.3k5 gold badges33 silver badges71 bronze badges asked Mar 12 at 1:57 user29966277user29966277 1 1 |1 Answer
Reset to default 0You're close! You just need to modify your ForEach
loop to iterate over the data
array instead of a fixed range. Additionally, ensure that fetchData()
is called to load data when the view appears.
struct ProjectViewPhotos: View {
@State private var data: [ProjectInfo] = []
let jobid: String
var body: some View {
ScrollView {
LazyVGrid(columns: [GridItem(.adaptive(minimum: 100, maximum: .infinity), spacing: 3)], spacing: 3) {
ForEach(data, id: \.id) { item in
VStack(alignment: .leading) {
NavigationLink(
destination: ViewImage(project_job_id: jobid, image_filename: item.image_filename)) {
AsyncImage(url: URL(string: "https://mywebsite/images/\(item.image_filename)"))
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
.clipped()
.aspectRatio(1, contentMode: .fit)
}
}
}
}
}
.navigationTitle("Project Photos - \(jobid)")
.onAppear {
fetchData()
}
}
func fetchData() {
guard let url = URL(string: "https://mywebsite/index.php?projectid=\(jobid)") else { return }
URLSession.shared.dataTask(with: url) { data, response, error in
guard let data = data else { return }
do {
let projectinfo = try JSONDecoder().decode([ProjectInfo].self, from: data)
DispatchQueue.main.async {
self.data = projectinfo
}
} catch {
print(error)
}
}.resume()
}
}
本文标签: swiftuiiOS Apptrying to load images from list in arrayStack Overflow
版权声明:本文标题:swiftui - iOS App - trying to load images from list in array - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744772581a2624430.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
ForEach
with indexes. UseForEach(data) { data in ... }
– Paulw11 Commented Mar 12 at 2:16