SwiftUI模型变更时视图未更新,如何更新List中现有行的内容?
问题根因
NotificationV是类(引用类型),父ViewModel的@Published修饰的数组只会监听数组本身的变更(如增删元素、替换元素),不会监听数组内部引用类型元素的属性变更,所以子元素属性修改后,父ViewModel不会发出视图刷新通知。- List循环中拿到的
notificationVV是ObservableObject类型,但没有用@ObservedObject包装,它自身的@Published属性变更也无法触发当前行的视图重绘。 - 你手动在
NotificationV里声明了objectWillChange,打断了ObservableObject默认的自动发布变更逻辑,也是导致刷新失效的原因之一。
解决方案
方案1(推荐):将NotificationV改为值类型(Struct)
结构体是值类型,元素属性变更相当于整个元素值变更,父ViewModel的@Published可以正常监听到数组变更触发刷新,代码改动量最小:
// 修改后的Model代码 struct NotificationV: Identifiable { let id = UUID() // 作为List的唯一标识,比用NotificationName更稳妥 var notificationName : String = "" var notificationDiscrip: String = "" var notifivationDate:String = "" var isShgowen:Bool = false init(notificationName: String, notificationDiscrip: String, notifivationDate: String, isShgowen: Bool) { self.notificationName = notificationName self.notificationDiscrip = notificationDiscrip self.notifivationDate = notifivationDate self.isShgowen = isShgowen } mutating func changTogle(){ if !isShgowen { isShgowen = true } } }
修改NotificationView的List遍历逻辑,用绑定方式修改数组元素:
List($notificationVModel.notificationarray) { $notificationVV in ZStack { if notificationVV.isShgowen { Color (.green).opacity(0.1) .cornerRadius(10) } HStack{ Button(action: { notificationVV.changTogle() print("\(notificationVV.isShgowen)") }, label: { ZStack{ Circle() .foregroundColor(Color(red: 0 / 255, green: 175 / 255, blue: 80 / 255)) .frame(width: 50, height: 50, alignment: .center) Image(systemName: "bell") .frame(width: 40, height: 40, alignment: .center) .foregroundColor(.white) } }) VStack{ HStack { if notificationVV.isShgowen{ Text("true") .font(.custom("AvenirNext-DemiBold", size: 10)) } Text(notificationVV.notificationName) .font(.custom("AvenirNext-DemiBold", size: 20)) Spacer() Text(notificationVV.notifivationDate) .font(.custom("AvenirNext-Medium", size: 12)) .foregroundColor(.gray) }.padding(.leading).padding(.trailing) Text(notificationVV.notificationDiscrip) .font(.custom("AvenirNext-Regular", size: 11)) .lineLimit(nil) .padding(.leading).padding(.trailing) } }.padding() } }
方案2:保留Class类型Model,手动处理监听
如果必须用类实现Model,需要做三处修改:
- 去掉NotificationV里手动声明的
objectWillChange,修复自动发布逻辑
class NotificationV : ObservableObject{ @Published var notificationName : String = "" @Published var notificationDiscrip: String = "" @Published var notifivationDate:String = "" @Published var isShgowen:Bool = false init(notificationName: String, notificationDiscrip: String, notifivationDate: String, isShgowen: Bool) { self.notificationName = notificationName self.notificationDiscrip = notificationDiscrip self.notifivationDate = notifivationDate self.isShgowen = isShgowen } func changTogle(){ if !isShgowen { isShgowen = true } } }
- List循环中用
@ObservedObject包装子元素,监听子元素属性变更
List(notificationVModel.notificationarray, id:\.notificationName) { notificationVV in @ObservedObject var item = notificationVV // 后续所有用到notificationVV的地方替换为item即可 }
- 父ViewModel监听所有子元素的变更事件,手动触发自身刷新
import Combine class NotificationVModel: ObservableObject { @Published var notificationarray : [NotificationV] = [ NotificationV(notificationName: "Notification 1", notificationDiscrip: "Lorem ipsum dolor sit amet,consec tetur adipiscing elit Lorem ipsum doloramet,consec tetur adipiscing elit sit ipi piscing… ", notifivationDate: "04/02/2021", isShgowen: false), NotificationV(notificationName: "Notification 2", notificationDiscrip: "Lorem ipsum dolor sit amet,consec tetur adipiscing elit Lorem ipsum doloramet,consec tetur adipiscing elit sit ipi piscing… ", notifivationDate: "05/03/2021", isShgowen: true), ] private var cancellables = Set<AnyCancellable>() init() { notificationarray.forEach { item in item.objectWillChange .sink(receiveValue: { [weak self] _ in self?.objectWillChange.send() }) .store(in: &cancellables) } } }
补充建议:Swift规范中属性名使用小驼峰命名,大驼峰仅用于类型命名,调整属性名可以避免后续潜在的逻辑错误。
内容的提问来源于stack exchange,提问作者Amine El hajjaji
相关产品推荐
相关产品推荐

