所以这很可能是一个 SwiftUI 新手问题,我有一个父视图,它采用 @ObservedObject(例如 viewModel)视图模型似乎正确地将模型中的更改发布到 CollectionView,并且视图正确地重新计算主体.但是,子视图的 (EmojiCell
) 主体似乎没有重新计算?以下是相关代码,感谢任何见解。
如果我将 Card 更改为 ObservableClass 并将其 isFaceUp 设置为 @Published,我可以让它工作,但这显然不是正确的解决方案!
struct CollectionView: View {
@ObservedObject var viewModel: EmojiMemoryGameViewModel
init(viewModel: EmojiMemoryGameViewModel) {
self.viewModel = viewModel
}
var body: some View {
// This is called on tap as expected so the viewModel is indeed
// correctly notifying the view of it's change, the body is
// recalculated, BUT EmojiCell's body doesn't get called!
print("CollectionView recalc")
return HStack {
ForEach(viewModel.cards) { card in
// This doesn't
EmojiCell(card: card).onTapGesture {
viewModel.choose(card: card)
}
// This works!
// return ZStack {
// if card.isFaceUp {
// RoundedRectangle(cornerRadius: 10)
// .fill(Color.white)
// RoundedRectangle(cornerRadius: 10)
// .stroke(lineWidth: 3)
// Text(card.content)
// }
// else {
// RoundedRectangle(cornerRadius: 10).fill()
// }
// }
// .onTapGesture {
// self.viewModel.choose(card: card)
// }
}
}
.padding()
.foregroundColor(.orange)
.font(.largeTitle)
}
}
typealias EmojiCard = MemoryGame<String>.Card
struct EmojiCell: View {
let card: EmojiCard
var body: some View {
print("Cell recalc")
return ZStack {
if card.isFaceUp {
RoundedRectangle(cornerRadius: 10)
.fill(Color.white)
RoundedRectangle(cornerRadius: 10)
.stroke(lineWidth: 3)
Text(card.content)
}
else {
RoundedRectangle(cornerRadius: 10).fill()
}
}
}
}
// Other relevant code
struct Card: Identifiable, Hashable {
var id: Int
var isFaceUp = true
var isMatched = false
var content: CardContent
func hash(into hasher: inout Hasher) {
hasher.combine(id)
}
}
extension MemoryGame.Card: Equatable {
static func == (lhs: MemoryGame<CardContent>.Card, rhs: MemoryGame<CardContent>.Card) -> Bool {
return lhs.id == rhs.id
}
}
class EmojiMemoryGameViewModel: ObservableObject {
@Published private var model: MemoryGame<String> = EmojiMemoryGameViewModel.createMemoryGame()
static func createMemoryGame() -> MemoryGame<String> {
let listOfCards = ["?", "?", "?"]
return MemoryGame(numberOfPairsOfCards: listOfCards.count) { index in
return listOfCards[index]
}
}
var cards: [MemoryGame<String>.Card] {
model.cards
}
func choose(card: MemoryGame<String>.Card) {
model.choose(card)
}
}
答案 0 :(得分:0)
这对我来说是一个愚蠢的错误。问题实际上是 Equatable 的不成熟实现,从 Swift 4.1+ 开始实际上不再需要它(Hashable 也是如此)。我最初添加它是为了确保我可以比较 Cards(需要 index(of:) 方法),但是,由于我只是检查比较 id 的,它与 SwiftUI 的内部比较算法用于重绘。 SwiftUI 也在使用我的 Equatable 实现并认为卡实际上并没有改变!