gpt4 book ai didi

Swift 如何为用户使用 Firebase 投票添加 5 星级评分

转载 作者:行者123 更新时间:2023-11-30 11:12:43 25 4
gpt4 key购买 nike

我正在 Swift 中创建一个餐厅评级应用程序,允许用户按照 5 星级评级标准对餐厅进行评级(就像 Zomato 和 Yelp)。我将使用 Firebase 存储每个用户对特定餐厅的评分,然后取用户评分的平均值并将其显示在应用中。每个tableview单元格都会有一个对应的星级(每个tableview单元格对应一个餐厅)。

然而,我对这个项目深陷其中。我不知道如何将用户的评分添加到数据库中。我的应用程序不需要用户注册或登录页面,用户可以直接对餐厅进行评分。话虽如此,我如何确保用户只能为一家餐厅投票一次?我将不胜感激任何帮助!预先感谢您。

这是迄今为止我的代码:

var starsRating = 0
var starsEmptyPicName = "star" // can change to empty star picture name
var starsFilledPicName = "starfill" // can change to filled star picture name
override func draw(_ rect: CGRect) {
let starButtons = self.subviews.filter{$0 is UIButton}
var starTag = 1
for button in starButtons {
if let button = button as? UIButton{
button.setImage(UIImage(named: starsEmptyPicName), for: .normal)
button.addTarget(self, action: #selector(self.pressed(sender:)), for: .touchUpInside)
button.tag = starTag
starTag = starTag + 1
}
}
setStarsRating(rating:starsRating)
}

func setStarsRating(rating:Int){
self.starsRating = rating
let stackSubViews = self.subviews.filter{$0 is UIButton}
for subView in stackSubViews {
if let button = subView as? UIButton{
if button.tag > starsRating {
button.setImage(UIImage(named: starsEmptyPicName), for: .normal)
}else{
button.setImage(UIImage(named: starsFilledPicName), for: .normal)
}
}
}
}

@objc func pressed(sender: UIButton) {
setStarsRating(rating: sender.tag)

}
}

这是我的表格 View 函数:

  func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return identifiers.count //identifiers is my big restaurants array
}



func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{

let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "CellThree")
cell.textLabel!.text = identifiers[indexPath.row]
return cell
}

最佳答案

如果用户已经投票,我将使用 CoreData 来保存。

  1. 当用户对其中一家餐厅进行评分时,只需在核心数据中使用该餐厅的名称或 ID 创建一条新记录即可。

  2. 在每次投票之前,我应该检查数据是否存在。如果没有,用户可以投票。

关于Swift 如何为用户使用 Firebase 投票添加 5 星级评分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52020942/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com