Swift如何使用Firebase为用户投票添加5星评级 [英] Swift how to add 5 star ratings for users to vote using Firebase

查看:67
本文介绍了Swift如何使用Firebase为用户投票添加5星评级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm creating a restaurant rating app in Swift that allows its users to rate restaurants on a 5-star rating scale (just like Zomato and Yelp) . I will be using Firebase to store each user's rating for a specific restaurant and then take the average of user ratings and display it in the app. There will be a star rating corresponding to each tableview cell (every tableview cell corresponds to a restaurant) .

但是,我对这个项目深感兴趣.我不知道如何将用户的评分添加到数据库中.我的应用程序不需要用户注册或登录页面,用户可以直接为餐厅评分.话虽这么说,我又如何确保用户只能为一家餐厅投票一次?我将不胜感激!预先谢谢你.

However I'm stuck deeply on the project. I can't figure out how to add the user's rating to the database. My app won't require a user sign-up or login page, users can directly rate restaurants. This being said, also, how can I make sure that users can vote only once for a single restaurant? I'd appreciate any help! Thank you in advance.

到目前为止,这是我的代码:

Here is my code so far:

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)

 }
} 

这是我的tableview函数:

And here are my tableview functions:

  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进行保存.

I would use CoreData to save if user already vote.

  1. 当用户对餐厅之一进行评分时,只需使用餐厅名称或ID在核心数据中创建新记录.

  1. When the user rate one of the restaurant, just create a new record in core data with the restaurant's name or ID.

在进行每次投票之前,我应该检查数据是否存在.否则,用户可以投票.

Before every vote I should check if data is existing or not. If not, user can vote.

这篇关于Swift如何使用Firebase为用户投票添加5星评级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆