Swift 如何在星级评分中显示评分值

Swift how to show rating value in star rating

我试图在 HCSStarRatingView 中显示来自 cocoa pods 的平均评分值。我有一种计算平均值的方法,现在我想通过平均值 Double 以在 Todays Rating 中显示正确的星级。我想知道我该怎么做? Todays Star Rating

这是平均评分

  class RatingTableViewCell: UITableViewCell {

@IBOutlet weak var dateLabel: UILabel!
var dateValue = Date()
var pastRating: Double = 0.0
@IBInspectable weak var rating: HCSStarRatingView!

var review: Workout?{
    didSet{
        configureCell()
    }
}
func configureCell(){
    if let review = review{
        review.appointment?.scheduled = dateValue
        let formatter = DateFormatter()
        formatter.dateFormat = "mm-dd-YYYY"
        let formattedDate = formatter.string(from: dateValue)
        dateLabel.text = formattedDate
        pastRating = 0.0
    }
}
override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
}

override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // Configure the view for the selected state
}

}

这里是 table 视图

 func numberOfSections(in tableView: UITableView) -> Int {
    return 30
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    guard let sections = fetchedResultsController.sections else {
        fatalError("No sections in fetchedResultsController")
    }
    return sections.count
}


func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let dequeuedCell = tableView.dequeueReusableCell(withIdentifier: "RatingTableViewCell", for: indexPath);
        guard let cell = dequeuedCell as? RatingTableViewCell else {
            return dequeuedCell
    }
    cell.review = self.fetchedResultsController.object(at: indexPath)

    return cell
}

我看过HCSSStarRatingView,有属性显示准确评分。所以改变你的代码:

if let review = review{
    review.appointment?.scheduled = dateValue
    let formatter = DateFormatter()
    formatter.dateFormat = "mm-dd-YYYY"
    let formattedDate = formatter.string(from: dateValue)
    dateLabel.text = formattedDate

    // You need to change code like this.
    rating.accurateHalfStars = true
    rating.value = <YourAverageRating>

    pastRating = 0.0    // I don't know about this, So can maintain it.
}

希望对您有所帮助。