我已经在我的应用程序中设置了一个“评论”部分,用户可以在其中选择他们希望在“1-5”级别(包括半星)上评分的星数。在我的Firebase数据库中选择的星的数量上一切都很好,当真正显示用户选择的星的数量时,问题就发生在这里。
例如,如果用户A选择了三星并提交它,当它出现在我的UITableViewCell中时,它将显示三星。。。那里一切都很好。
如果另一个用户,比如说用户B选择了4星并提交了它,那么我的UITableViewCell将更改用户A的评论中显示的星,所以现在它看起来(在我的firebase数据库中没有任何更改,它仍然将其读取为4星和3星级别)好像有两个4星级别。
如果说不清楚的话,基本上就像是尖叫
这是我试过的代码:

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)


                }
            }
        }
    }

}

这在我的桌面视图里
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)
}

最佳答案

我建议在单元格中有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

关于ios - 如何在tableViewCell中获得准确的星星,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42209597/

10-12 00:30