如何在tableViewCell中获得准确的星星

时间:2017-02-13 17:06:41

标签: ios swift uitableview tableviewcell

我在我的应用程序中设置了一个“评论”部分,用户可以选择他们想要在“1-5”等级上评分的星数,包括半星。当实际显示用户选择的星星数量时,一切正常,可以将选定的星数选入我的Firebase数据库,这是故障发生的地方。

例如,如果用户A选择了3颗星并提交它,当它出现在我的UITableViewCell中时,它会显示3颗星......那里的一切都很好。

如果另一个用户,让我们说用户B选择4星并提交它,我的UITableViewCell会更改用户A评论中显示的星星,所以现在它看起来(我的firebase数据库中的任何内容都没有更改它仍然将其读取为一个4星级和3星评级)好像有两个4星评级。

如果不清楚,那基本上就像Yelp

这是我尝试过的代码:

let half = UIImage(named: "zeroPointFive")
let full = UIImage(named: "selectedReviewStar")

if let x = review?.ratingNumber?.floatValue{
                let number = String(x)
                someReviewStars.text = number

 //my attempt at using a switch to solve it

                switch x {
                case 0.50 : halfStar.image = half;print("we have a 0.5 here", x)

                case 1.0 : halfStar.image = full;print("we have a 1.0 here", x)
                default: return
            }


//I created different views for each star and added a subview image to them

  if someReviewStars.text == "0.0"{
                   someReviewStars.text = "No Stars"
                }


                if someReviewStars.text == "0.5"{
                   someReviewStars.text = ""
                    halfStar.image = half

                }

                if someReviewStars.text == "1.0"{
                    someReviewStars.text = ""
                  halfStar.isHidden = true
                  oneStar.image = full


                }
                if someReviewStars.text == "1.5"{
                    someReviewStars.text = ""
                    halfStar.isHidden = true
                    oneStar.image = full
                    onePointFiveStar.image = half

                }


                if someReviewStars.text == "2.0"{
                    someReviewStars.text = ""
                    halfStar.isHidden = true
                    oneStar.image = full
                    onePointFiveStar.isHidden = true
                    twoStar.image = full
                }


                if someReviewStars.text == "2.5"{
                    someReviewStars.text = ""
                    let oneStarFrame = CGRect(x: -226, y: 0, width: 19, height: 17)
                    let oneStarBackgroundImage = UIImageView(frame: oneStarFrame)
                    oneStarBackgroundImage.image = full

                    self.oneStar.insertSubview(oneStarBackgroundImage, at: 0)


                    let twoStarsFrame = CGRect(x: -206, y: 0, width: 17, height: 17)
                    let twoStarsBackgroundImage = UIImageView(frame: twoStarsFrame)
                    twoStarsBackgroundImage.image = full

                    self.twoStar.insertSubview(twoStarsBackgroundImage, at: 0)

                    let twoPointFiveFrame = CGRect(x: -186, y: 0, width: 10, height: 17)
                    let twoPointFiveBackgroundImage = UIImageView(frame: twoPointFiveFrame)
                    twoPointFiveBackgroundImage.image = half

                    self.twoPointFiveStar.insertSubview(twoPointFiveBackgroundImage, at: 0)


                }



                if someReviewStars.text == "3.0"{
                    someReviewStars.text = ""
                    let oneStarFrame = CGRect(x: -226, y: 0, width: 19, height: 17)
                    let oneStarBackgroundImage = UIImageView(frame: oneStarFrame)
                    oneStarBackgroundImage.image = full

                    self.oneStar.insertSubview(oneStarBackgroundImage, at: 0)


                    let twoStarsFrame = CGRect(x: -206, y: 0, width: 17, height: 17)
                    let twoStarsBackgroundImage = UIImageView(frame: twoStarsFrame)
                    twoStarsBackgroundImage.image = full

                    self.twoStar.insertSubview(twoStarsBackgroundImage, at: 0)

                    let theframe = CGRect(x: -186, y: 0, width: 19, height: 17)
                    self.threeStar.image?.draw(in: theframe)
                    self.threeStar.image = full


                }
                if someReviewStars.text == "3.5"{
                    someReviewStars.text = ""
                    let oneStarFrame = CGRect(x: -226, y: 0, width: 19, height: 17)
                    let oneStarBackgroundImage = UIImageView(frame: oneStarFrame)
                    oneStarBackgroundImage.image = full

                    self.oneStar.insertSubview(oneStarBackgroundImage, at: 0)


                    let twoStarsFrame = CGRect(x: -206, y: 0, width: 17, height: 17)
                    let twoStarsBackgroundImage = UIImageView(frame: twoStarsFrame)
                    twoStarsBackgroundImage.image = full

                    self.twoStar.insertSubview(twoStarsBackgroundImage, at: 0)

                    let threeFrame = CGRect(x: -186, y: 0, width: 19, height: 17)
                    let threeBackgroundImage = UIImageView(frame: threeFrame)
                    threeBackgroundImage.image = full

                    self.threeStar.insertSubview(threeBackgroundImage, at: 0)

                    let threePointFiveFrame = CGRect(x: -167, y: 0, width: 10, height: 17)
                    let threePointFiveBackgroundImage = UIImageView(frame: threePointFiveFrame)
                    threePointFiveBackgroundImage.image = half

                    self.threePointFiveStar.insertSubview(threePointFiveBackgroundImage, at: 0)


                }
                if someReviewStars.text == "4.0"{
                    someReviewStars.text = ""
                    let oneStarFrame = CGRect(x: -226, y: 0, width: 19, height: 17)
                    let oneStarBackgroundImage = UIImageView(frame: oneStarFrame)
                    oneStarBackgroundImage.image = full

                    self.oneStar.insertSubview(oneStarBackgroundImage, at: 0)


                    let twoStarsFrame = CGRect(x: -206, y: 0, width: 17, height: 17)
                    let twoStarsBackgroundImage = UIImageView(frame: twoStarsFrame)
                    twoStarsBackgroundImage.image = full

                    self.twoStar.insertSubview(twoStarsBackgroundImage, at: 0)

                    let threeFrame = CGRect(x: -186, y: 0, width: 19, height: 17)
                    let threeBackgroundImage = UIImageView(frame: threeFrame)
                    threeBackgroundImage.image = full

                    self.threeStar.insertSubview(threeBackgroundImage, at: 0)

                    let fourStarsFrame = CGRect(x: -167, y: 0, width: 19, height: 17)
                    let fourStarsBackgroundImage = UIImageView(frame: fourStarsFrame)
                    fourStarsBackgroundImage.image = full

                    self.fourStar.insertSubview(fourStarsBackgroundImage, at: 0)


                }
                if someReviewStars.text == "4.5"{
                    someReviewStars.text = ""
                    let oneStarFrame = CGRect(x: -226, y: 0, width: 19, height: 17)
                    let oneStarBackgroundImage = UIImageView(frame: oneStarFrame)
                    oneStarBackgroundImage.image = full

                    self.oneStar.insertSubview(oneStarBackgroundImage, at: 0)


                    let twoStarsFrame = CGRect(x: -206, y: 0, width: 17, height: 17)
                    let twoStarsBackgroundImage = UIImageView(frame: twoStarsFrame)
                    twoStarsBackgroundImage.image = full

                    self.twoStar.insertSubview(twoStarsBackgroundImage, at: 0)

                    let threeFrame = CGRect(x: -186, y: 0, width: 19, height: 17)
                    let threeBackgroundImage = UIImageView(frame: threeFrame)
                    threeBackgroundImage.image = full

                    self.threeStar.insertSubview(threeBackgroundImage, at: 0)

                    let fourStarsFrame = CGRect(x: -167, y: 0, width: 19, height: 17)
                    let fourStarsBackgroundImage = UIImageView(frame: fourStarsFrame)
                    fourStarsBackgroundImage.image = full

                    self.fourStar.insertSubview(fourStarsBackgroundImage, at: 0)

                    let fourPointFiveFrame = CGRect(x: -146, y: 0, width: 10, height: 17)
                    let fourPointFiveBackgroundImage = UIImageView(frame: fourPointFiveFrame)
                    fourPointFiveBackgroundImage.image = half

                    self.fourPointFiveStar.insertSubview(fourPointFiveBackgroundImage, at: 0)


                }
                if someReviewStars.text == "5.0"{
                    someReviewStars.text = ""
                    let oneStarFrame = CGRect(x: -226, y: 0, width: 19, height: 17)
                    let oneStarBackgroundImage = UIImageView(frame: oneStarFrame)
                    oneStarBackgroundImage.image = full

                    self.oneStar.insertSubview(oneStarBackgroundImage, at: 0)


                    let twoStarsFrame = CGRect(x: -206, y: 0, width: 17, height: 17)
                    let twoStarsBackgroundImage = UIImageView(frame: twoStarsFrame)
                    twoStarsBackgroundImage.image = full

                    self.twoStar.insertSubview(twoStarsBackgroundImage, at: 0)

                    let threeFrame = CGRect(x: -186, y: 0, width: 19, height: 17)
                    let threeBackgroundImage = UIImageView(frame: threeFrame)
                    threeBackgroundImage.image = full

                    self.threeStar.insertSubview(threeBackgroundImage, at: 0)

                    let fourStarsFrame = CGRect(x: -167, y: 0, width: 19, height: 17)
                    let fourStarsBackgroundImage = UIImageView(frame: fourStarsFrame)
                    fourStarsBackgroundImage.image = full

                    self.fourStar.insertSubview(fourStarsBackgroundImage, at: 0)

                    let fiveStarsFrame = CGRect(x: -146, y: 0, width: 19, height: 17)
                    let fiveStarsBackgroundImage = UIImageView(frame: fiveStarsFrame)
                    fiveStarsBackgroundImage.image = full

                    self.fiveStar.insertSubview(fiveStarsBackgroundImage, at: 0)


                }
            }
        }
    }

}

这是我的tableView

class loadingTableReview: UITableViewController {


let cellId = "cellId"
var selectedUsersProfile:User?

override func viewDidLoad() {
    super.viewDidLoad()


tableView.register(UserCell.self, forCellReuseIdentifier: cellId)

    observeUserReviews()

}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return reviews.count
}



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

    let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! UserCell
    let review = reviews[indexPath.row]
     cell.review = review


    return cell
}


var reviews = [Reviews]()
var reviewDictionary = [String: Reviews]()



func observeUserReviews(){
    guard let uid = selectedUsersProfile?.id

        else{
    return
    }
    let ref = FIRDatabase.database().reference().child("user-reviews").child(uid)
    ref.observe(.childAdded, with: {(snapshot) in


        let reviewId = snapshot.key
        let messageReference = FIRDatabase.database().reference().child("reviews").child(reviewId)

        messageReference.observeSingleEvent(of: .value, with: {(snapshot) in
            print(snapshot)

                if let dictionary = snapshot.value as?[String: AnyObject]{
                let review = Reviews()
                review.setValuesForKeys(dictionary)


                if let fromId = review.fromId{
                    self.reviewDictionary[fromId] = review

                    self.reviews.append(review)
                    self.reviews.sort(by: { (review1, review2) -> Bool in
                        return (review1.timeStamp?.intValue)! > (review2.timeStamp?.intValue)!
                    })

                }

                self.tableView.reloadData()

            }

        }, withCancel: nil)

    }, withCancel: nil)
}

1 个答案:

答案 0 :(得分:1)

我建议在单元格中有5个固定的图像视图,并根据评级值系统地设置其图像属性

例如:

 starImage1.image = rating < 0.5 ? nil : rating < 1 ? half : full
 starImage2.image = rating < 1.5 ? nil : rating < 2 ? half : full
 starImage3.image = rating < 2.5 ? nil : rating < 3 ? half : full
 starImage4.image = rating < 3.5 ? nil : rating < 4 ? half : full
 starImage5.image = rating < 4.5 ? nil : rating < 5 ? half : full