将元素追加到数组后,它应该显示更多元素 [英] After appending element to array it should show more elements
本文介绍了将元素追加到数组后,它应该显示更多元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个ForEach循环,当我单击该卡时,它会向阵列添加一个新元素,然后它应该显示2张卡.但是仍然有一张卡.(但我附加了新的卡片元素)那我该怎么办?
I have a ForEach Loop, and when I click on the card, it appends a new element to the array and then IT SHOULD SHOW 2 CARDS. But there is still one card. (but I appended the new card element) So what should I do?
struct TimelineFromUserView: View {
var card: [Card] = cardData
var body: some View {
VStack {
HStack {
Text("History").bold()
.font(.largeTitle)
.padding(.top, 20)
.padding(.leading, 20)
Spacer()
}
Divider()
ScrollView(.vertical, showsIndicators: false) {
VStack(spacing: 30) {
ForEach(card) { item in
CardForTimeline(card: item)
.onTapGesture {
print("juhuu")
cardData.append(Card(number: 2, start: "01.10.2021", end: "20.10.2021", days: 19, success: false))
print(cardData)
}
.padding()
}
}
.padding(.top, 10)
}
}
}
}
struct Card: Identifiable{
var id = UUID()
var number: Int
var start: String
var end: String
var days: Int
var success: Bool
}
var cardData: [Card] = [
Card(number: 1 ,start: "05.06.2020", end: "15.06.2020", days: 10, success: true)
]
推荐答案
您只能将数组 cardData
的副本 card
传递给 ForEach
,但如果我正确理解了您的代码段,则该代码段应该是原始数组 cardData
.
You only pass a copy card
of the array cardData
into the ForEach
but if I understand your code snippets correctly it should be the original array cardData
.
如果这不是问题,则发布更多代码将很有帮助.
If that's not the problem it would be helpful to post more of your code.
这篇关于将元素追加到数组后,它应该显示更多元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文