简体   繁体   English

使用 ForEach 循环两个 arrays 不同类型的项目(字符串和图像) SwiftUI

[英]Looping two arrays of different type items (string and image) with ForEach SwiftUI

In stead to have an array of image under an array of text, I would like to have one array alternatively with image or text depending if I push the button text or the button picture.取而代之的是在一组文本下放置一组图像,我希望有一个数组交替显示图像或文本,具体取决于我是按下按钮文本还是按钮图片。

Here is what I have tried:这是我尝试过的:

struct HistoryPicture: Identifiable {let id = UUID ()
var picture: Image?}

struct HistoryText: Identifiable {let id = UUID ()
    var text: String?}

struct ContentView: View {

@State var archivePicture: [HistoryPicture] = []
@State var archiveText: [HistoryText] = []

var body: some View {ScrollView{

        VStack(spacing:20){
            
                ForEach(self.archiveText) { item in
                        Text("<text>")
                        }

                ForEach(self.archivePicture) { item in
                    Image("simpson")
                        }
      // Button Text

            HStack{

                Button {var newHistory = HistoryText()
                    newHistory.text = "<text>"
                   self.archiveText.append(newHistory)

                } label: { Circle()
                             .fill(Color.black)
                             .frame(width: 70, height: 70)} }

    // Button Picture

            HStack{

                Button {var newHistoryPicture = HistoryPicture()
                        newHistoryPicture.picture =     Image("simpson")
                        self.archivePicture.append(newHistoryPicture)

                    } label: { Circle()
                                 .fill(Color.red)
                                 .frame(width: 70, height: 70)
                    }} }} }}

With this solution you get the results in the order the buttons were pressed.使用此解决方案,您可以按照按下按钮的顺序获得结果。 For that you just need one ForEach with one struct.为此,您只需要一个 ForEach 和一个结构。 If you want more complex views inside the ForEach, I would suggest using Protocols or inheritance.如果您想在 ForEach 中使用更复杂的视图,我建议您使用 Protocols 或 inheritance。

struct HistoryItem: Identifiable {
    let id = UUID()
    var text: String?
    var imageName: String?
}

struct ContentView: View {
    @State var items = [HistoryItem]()
    
    var body: some View {
        ScrollView {
            VStack(spacing: 20){
                ForEach(items) { item in
                    if let text = item.text {
                        Text(text)
                    } else if let imageName = item.imageName {
                        Image(imageName)
                    }
                }
                
                HStack {
                    Button {
                        let newItem = HistoryItem(text: "<text>")
                        items.append(newItem)
                    } label: {
                        Circle()
                            .fill(Color.black)
                            .frame(width: 70, height: 70)
                    }
                    
                    Button {
                        let newItem = HistoryItem(imageName: "simpson")
                        items.append(newItem)
                    } label: {
                        Circle()
                            .fill(Color.red)
                            .frame(width: 70, height: 70)
                    }
                }
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM