swift - 如何像stackOverFlow一样增加/减少UpVote/DownVote?
问题描述
我有这段代码,它允许用户像堆栈溢出一样进行赞成或反对。我的问题是Text("\(likes) UpVote").padding(.top, 8)
不会立即刷新,而是用户应该更改视角以获得刷新的赞成/反对票。如何改进这一点,以便用户只能单击其中一个并立即更改它们?
这就是我的代码的样子:
import SwiftUI
import SDWebImageSwiftUI
import Firebase
struct PostCellCard : View {
var user = ""
var image = ""
var id = ""
var likes = ""
var comments = ""
var msg = ""
var body : some View{
VStack(alignment: .leading, content: {
HStack{
Image("person-icon-1675").resizable().frame(width: 35, height: 35).clipShape(Circle()).onTapGesture {
print("slide out menu ....")
}
HStack(alignment: .top){
VStack(alignment: .leading){
Text(user).fontWeight(.heavy)
Text(msg).padding(.top, 8)
}}
Spacer()
Button(action: {
}) {
Image("menu").resizable().frame(width: 15, height: 15)
}.foregroundColor(Color("darkAndWhite"))
}
if self.image != ""{
AnimatedImage(url: URL(string: image)).resizable().frame(height: 250)
}
HStack{
Button(action: {
let db = Firestore.firestore()
let like = Int.init(self.likes)!
db.collection("posts").document(self.id).updateData(["likes": "\(like - 1)"]) { (err) in
if err != nil{
print((err)!)
return
}
print("down updated....")
}
}) {
Image(systemName: "arrow.down.to.line.alt").resizable().frame(width: 26, height: 26)
}.foregroundColor(Color("darkAndWhite"))
Button(action: {
let db = Firestore.firestore()
let like = Int.init(self.likes)!
db.collection("posts").document(self.id).updateData(["likes": "\(like + 1)"]) { (err) in
if err != nil{
print((err)!)
return
}
print("up updated....")
}
}) {
Image(systemName: "arrow.up.to.line.alt").resizable().frame(width: 26, height: 26)
}.foregroundColor(Color("darkAndWhite"))
Spacer()
}.padding(.top, 8)
Text("\(likes) UpVote").padding(.top, 8)
Text("\(likes) DownVote").padding(.top, 8)
Text("View all \(comments) Comments")
}).padding(8)
}
}
解决方案
不幸的是,我必须进行很多更改才能运行...所以这是我的代码,它可以工作-更改喜欢-当然,您可以从中创建一个 int 并增加它,而不仅仅是设置文本
struct ContentView : View {
var user = ""
var image = ""
var id = ""
@State var likes = ""
var comments = ""
var msg = ""
var body : some View{
VStack {
HStack{
Image(systemName:"circle").resizable().frame(width: 35, height: 35).clipShape(Circle()).onTapGesture {
print("slide out menu ....")
self.likes = "tapped"
}
HStack(alignment: .top){
VStack(alignment: .leading){
Text(user).fontWeight(.heavy)
Text(msg).padding(.top, 8)
}}
Spacer()
Button(action: {
self.likes = "aha"
}) {
Image(systemName:"circle").resizable().frame(width: 15, height: 15)
}.foregroundColor(Color("darkAndWhite"))
}
if self.image != ""{
Text("Animated Image")
// AnimatedImage(url: URL(string: image)).resizable().frame(height: 250)
}
HStack{
Button(action: {
self.likes = "oho"
print("button action")
}) {
Image(systemName: "circle").resizable().frame(width: 26, height: 26)
}.foregroundColor(Color("darkAndWhite"))
Button(action: {
let like = Int.init(self.likes)!
print("action")
self.likes = "uhu"
}) {
Text("aha")
// Image(systemName: "arrow.up.to.line.alt").resizable().frame(width: 26, height: 26)
}.foregroundColor(Color("darkAndWhite"))
Spacer()
}.padding(.top, 8)
Text("\(likes) UpVote").padding(.top, 8)
Text("\(likes) DownVote").padding(.top, 8)
Text("View all \(comments) Comments")
}.padding(8)
}
}
推荐阅读
- python - 我不能在嵌套模式下使用我自定义的 Django 管理器方法
- javascript - 为透明图像设置后备的最佳方法是什么?
- npm - `npm run prod` 去除导入的 css
- java - javapoet如何生成注释和使用lombok?
- python - 连接两列值并根据 Python 中的特定列创建连续的唯一 ID
- dataset - 我试图上传我的 csv 文件数据集,但 weka 无法识别它
- sql - 减少数量直到我用完所有数量
- c++ - 在运行时 C++ 设置类字段大小
- multithreading - Recursive impl Extend for HashMap<_, HashSet<_>> 在哪里组合集合,而不是覆盖?
- jquery - jQuery:使用输入的值作为匹配数组的名称