我具有以下视图层次结构
Nurse List View > Nurse Card > Favorite button
护士列表视图
struct NurseListView: View {
@State var data: [Nurse] = []
var body: some View {
List {
ForEach(data.indices, id: \.self) { index in
NurseCard(data: self.$data[index])
}
}
}
}
护士卡
struct NurseCard: View {
@Binding var data: Nurse
var body: some View {
FavoriteActionView(data:
Binding(
get: { self.data },
set: { self.data = $0 as! Nurse }
)
)
}
}
收藏夹操作视图
struct FavoriteActionView: View {
@Binding var data: FavoritableData
var body: some View {
Button(action: {
self.toggleFavIcon()
}) {
VStack {
Image(data.isFavorite ? "fav-icon" : "not-fav-icon")
Text(String(data.likes.count))
}
}
}
private func toggleFavIcon() {
if data.isFavorite {
if let index = data.likes.firstIndex(of: AppState.currentUser.uid) {
data.likes.remove(at: index)
}
} else {
data.likes.append(AppState.currentUser.uid)
}
}
}
执行toggleFavIcon
时,它会从数据对象的likes
属性中添加/删除用户ID,但是除非返回上一页并重新打开页面,否则看不到更改。我在这里想念什么?
答案 0 :(得分:0)
正如Asperi所写,在这里使用ObservableObject可以很好地工作。像这样:
elements = (1..3).map do |x|
b.element(
xpath: '/html/body/form/div[3]/div[6]/table/tbody/tr/td/div[2]/div[3]/div[3]/div/div/div[1]/table/tbody/tr[%d]/td[13]/span' % x
)
end.select(&:exists?)