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

时间:2018-03-28 13:08:22

标签: swift uiview cocoapods rating

我试图在可可豆荚中显示HCSStarRatingView的平均评级值。我有一个方法可以计算平均值,现在我希望通过平均值Double来显示今日评级中的正确星级。我想知道我该怎么做? 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
}

}

这是表格视图

 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
}

1 个答案:

答案 0 :(得分:0)

我见过 HCSStarRatingView ,有给出的属性可以显示准确的评分。所以改变你的代码:

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.
}

我希望这会对你有所帮助。