【发布时间】:2020-09-29 07:33:31
【问题描述】:
我是编码新手,目前正在学习使用 SwiftUI 的 instagram 教程,为了提高我的技能,我一直在调整我的布局以尝试新概念。
我目前遇到“更新点赞数”的问题。本教程显示了 likeCount 变量的典型 instagram 布局,它是点赞按钮下“页脚”(图像下方)的一部分,但我已经采用了方法(类似于 Vero),在标题和点赞按钮上有了点赞按钮数到页脚。
查看当前代码:
import Foundation
import SwiftUI
import FirebaseAuth
class HeaderCellViewModel: ObservableObject {
@Published var post: Post!
@Published var isLoading = false
@Published var isLiked = false
func checkPostIsLiked() {
isLiked = (post.likes["\(Auth.auth().currentUser!.uid)"] == true) ? true : false
}
func like() {
post.likeCount += 1
isLiked = true
Ref.FIRESTORE_MY_POSTS_DOCUMENT_USERID(userId: post.ownerId).collection("userPosts").document(post.postId).updateData(["likes.\(Auth.auth().currentUser!.uid)" : true,
"likeCount": post.likeCount])
Ref.FIRESTORE_COLLECTION_ALL_POSTS.document(post.postId).updateData(["likes.\(Auth.auth().currentUser!.uid)" : true,
"likeCount": post.likeCount])
Ref.FIRESTORE_TIMELINE_DOCUMENT_USERID(userId: post.ownerId).collection("timelinePosts").document(post.postId).updateData(["likes.\(Auth.auth().currentUser!.uid)" : true,
"likeCount": post.likeCount])
}
func unlike() {
post.likeCount -= 1
isLiked = false
Ref.FIRESTORE_MY_POSTS_DOCUMENT_USERID(userId: post.ownerId).collection("userPosts").document(post.postId).updateData(["likes.\(Auth.auth().currentUser!.uid)" : false,
"likeCount": post.likeCount])
Ref.FIRESTORE_COLLECTION_ALL_POSTS.document(post.postId).updateData(["likes.\(Auth.auth().currentUser!.uid)" : false,
"likeCount": post.likeCount])
Ref.FIRESTORE_TIMELINE_DOCUMENT_USERID(userId: post.ownerId).collection("timelinePosts").document(post.postId).updateData(["likes.\(Auth.auth().currentUser!.uid)" : false,
"likeCount": post.likeCount])
}
}
import SwiftUI
import SwiftUI
import URLImage
struct HeaderCell: View {
@ObservedObject var headerCellViewModel = HeaderCellViewModel()
init(post: Post) {
self.headerCellViewModel.post = post
self.headerCellViewModel.checkPostIsLiked()
}
if headerCellViewModel.post.likeCount > 0 {
Text("\(headerCellViewModel.post.likeCount)")
}
Image(systemName: (self.headerCellViewModel.isLiked) ? "hand.thumbsup.fill" : "hand.thumbsup").resizable().scaledToFill().frame(width: 30, height: 30).foregroundColor(.gray).onTapGesture {
if self.headerCellViewModel.isLiked {
self.headerCellViewModel.unlike()
} else {
self.headerCellViewModel.like()
}
}
struct FooterCell: View {
@ObservedObject var footerCellViewModel = FooterCellViewModel()
@ObservedObject var headerCellViewModel = HeaderCellViewModel()
init(post: Post) {
self.footerCellViewModel.post = post
self.footerCellViewModel.checkPostIsLiked()
}
var body: some View {
VStack(alignment: .leading, spacing: 8) {
Text(self.footerCellViewModel.post.caption).font(.subheadline).padding(.leading)
HStack {
Image(systemName: "mappin.and.ellipse").foregroundColor(.gray).imageScale(.small)
Text("Location").font(.caption).foregroundColor(.gray)
}.padding(.leading)
HStack {
NavigationLink(destination: CommentView(post: self.footerCellViewModel.post)) {
Text("4K")
Image(systemName: "text.bubble").renderingMode(.original)
}
Spacer().frame(width: 15)
NavigationLink(destination: Text("Likes")) {
if footerCellViewModel.post.likeCount > 0 {
Text("\(footerCellViewModel.post.likeCount)")
} else {
Text("0")
}
Image(systemName: "hand.thumbsup").renderingMode(.original)
}
我的问题是,当我从 footerCell 引用 headerCellViewModel 时,它会出现“线程 1:EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)”并崩溃。我复制了 headerCellViewModel 并将其设置为页脚,但是当我现在按下“赞”按钮时,视图计数不会实时更新。他们只是不互相交谈,我不确定该怎么做。
【问题讨论】:
标签: xcode variables swiftui instagram