【发布时间】:2018-08-25 20:18:59
【问题描述】:
我正在 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)
}
}
这是我的 tableview 函数:
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
}
【问题讨论】:
-
这是问题我的应用不需要用户注册或登录页面。如果没有用户登录或某种身份验证,就无法跟踪它。应用程序需要知道谁在投票并将该数据存储在 /already_voted/ 节点中。然后查看该用户是否已经在代码中投票,或者如果他们已经投票,则利用 Firebase 规则拒绝写入。您可以使用Anonymous Authentication 来获得一些可追溯性,但这并不是一个万无一失的方法。最好创建用户并存储他们的 uid。
标签: swift uitableview firebase firebase-realtime-database