如何强制视图从已发布数组项目的价值变化中进行更新
免责声明:这里的Android开发人员
一直试图通过KMM在iOS上进行现有的Android应用程序工作,我想知道是否有一种方法可以强制SwiftUI的视图更新,而仅进行更新才能进行更新数组中的项目的参数。 (类似于 array [index] .copy()函数在JetPack中撰写
此处是设置:
class MyViewModel: ObservableObject {
...
@Published var itemsArray = [Item]()
...
func increaseCount(itemIndex: Int) {
let count = itemsArray[itemIndex].quantity + 1
itemsArray[itemIndex].quantity = count
print("item count is \(count)")
}
func decreaseCount(itemIndex: Int) {
var count: Int
if itemsArray[itemIndex].quantity < 2 {
count = 1
} else {
count = Int(itemsArray[itemIndex].quantity - 1)
}
itemsArray[itemIndex].quantity = Int32(count) //Xcode insisted on Int32
}
...
}
在有问题的视图上:
struct SecondView: View {
@ObservedObject var viewModel: MyViewModel
...
var body: some View {
...
ForEach (viewModel.itemsArray, id: \.self) {
item in
let index = viewModel.itemsArray.firstIndex(of: item)
HStack {
Button(action: {viewModel.decreaseCount(itemIndex: index!
)}) {
Image(systemName: "minus.circle")
.foregroundColor(Color.gray)
}
Text("\(viewModel.itemsArray[index!].quantity)")
Button(action: {viewModel.increaseCount(itemIndex: index!)}) {
Image(systemName: "plus.circle")
.foregroundColor(Color.black)
}
}
}
}
}
我还在
extension Item: Identifiable{}
包含ViewModel的文件上
设置了ViewModel中的print语句 但UI没有任何帮助。
进行更新, 成为一种更高效/优雅的方式,在迅速吧?
Disclaimer: Android developer here
Been trying to make an existing android app work on iOS via KMM and I'd like to know if there's a way to force a view update in SwiftUI when only an update is made to an Item's paramaters in an array. (Something like the array[index].copy() function in Jetpack Compose
Here's the set up:
class MyViewModel: ObservableObject {
...
@Published var itemsArray = [Item]()
...
func increaseCount(itemIndex: Int) {
let count = itemsArray[itemIndex].quantity + 1
itemsArray[itemIndex].quantity = count
print("item count is \(count)")
}
func decreaseCount(itemIndex: Int) {
var count: Int
if itemsArray[itemIndex].quantity < 2 {
count = 1
} else {
count = Int(itemsArray[itemIndex].quantity - 1)
}
itemsArray[itemIndex].quantity = Int32(count) //Xcode insisted on Int32
}
...
}
On the View in question:
struct SecondView: View {
@ObservedObject var viewModel: MyViewModel
...
var body: some View {
...
ForEach (viewModel.itemsArray, id: \.self) {
item in
let index = viewModel.itemsArray.firstIndex(of: item)
HStack {
Button(action: {viewModel.decreaseCount(itemIndex: index!
)}) {
Image(systemName: "minus.circle")
.foregroundColor(Color.gray)
}
Text("\(viewModel.itemsArray[index!].quantity)")
Button(action: {viewModel.increaseCount(itemIndex: index!)}) {
Image(systemName: "plus.circle")
.foregroundColor(Color.black)
}
}
}
}
}
I am also setting
extension Item: Identifiable{}
on the file containing the ViewModel
The print statements in the viewmodel get updated according but the UI does not. Any help will be appreciated.
EDIT: Just realised that if I remove the element at the specified index and re-insert it, the UI updates. Surely there has to be a more efficient/ elegant way to this is on Swift, right?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
没有所有相关的代码,我只能猜测。尝试这样的事情:
并且不需要您的
增加
和depeSecount
在您的myViewModel
中。EDIT-1:这是我的测试代码,该代码显示在按下/单击按钮时更新UI。
without all relevent the code, I can only guess. Try something like this:
and no need for your
increaseCount
anddecreaseCount
code in yourMyViewModel
.EDIT-1: here is my test code that shows the UI is updated when the buttons are tapped/clicked.