如何使用代表实施Cosmos星级评分?

时间:2017-11-29 13:44:42

标签: ios swift uitableview delegates

在包含2个部分的UITableView中,我想实现CosmosView star rating 在第一部分中,我创建了一个原型单元格并将其分配给自定义类protocol ClassFeedbackCellDelegate

然后我实施DetailViewController,必须为DetailViewController提供已点击的单元格   问题是我没有在func sharePressed(cell: FeedbackCell)的{​​{1}}中获得评分值。我根据这个答案实现了协议。 how to call presentViewController from within a UICollectionViewCell
    请指导我如何正确实施此评级控制。

protocol ClassFeedbackCellDelegate {
   func sharePressed(cell: FeedbackCell)
}

class FeedbackCell: UITableViewCell {

  var delegate: ClassFeedbackCellDelegate?
  @IBOutlet weak var cosmosViewBelongingToFeedBackCell: CosmosView!
  @IBOutlet weak var submitButtonOutlet: UIButton!

  @IBAction func submitFeedBack(_ sender: Any) {
    delegate?.sharePressed(cell: self)
   }
}




 class DetailViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

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

     let cell = tableView.dequeueReusableCell(withIdentifier: "ContentCell")
     let feedbackCell = tableView.dequeueReusableCell(withIdentifier: "FeedbackCell") as? FeedbackCell
    feedbackCell?.delegate = self  //assign self as the delegate

    if bookingCompleted && indexPath.section == 0 {

        //default rating when tableView loads for first time
        feedbackCell?.cosmosViewBelongingToFeedBackCell.rating = 4
        return feedbackCell ?? UITableViewCell()
    }

        (cell?.contentView.viewWithTag(1) as? UILabel)?.text = titles[indexPath.row]
        (cell?.contentView.viewWithTag(2) as? UILabel)?.text = titlesToValues[titles[indexPath.row]]
           return cell ?? UITableViewCell()
  }
}

extension DetailViewController: ClassFeedbackCellDelegate {

 func sharePressed(cell: FeedbackCell) {
     print("sharePressed was called ")// prints out
     cell.cosmosViewBelongingToFeedBackCell.didFinishTouchingCosmos = {
      rating in
            print("rating here in extension is \(rating)")//doesn't print out
     }
   }
}

enter image description here

1 个答案:

答案 0 :(得分:1)

根据文件:

  

//当用户从视图中抬起手指完成更改评级时调用。   cosmosView.didFinishTouchingCosmos = {rating in}

因此,当调用sharePressed(cell: FeedbackCell)时,用户已经抬起手指,因此这不符合您的需要。

相反,您可以这样做:

func sharePressed(cell: FeedbackCell) {
    print("sharePressed was called ")// prints out
    print("rating: \(cell.cosmosViewBelongingToFeedBackCell.rating)")
}

此外,如果用户滚动表格视图,您应该将评级保存在某个位置。

修改

如果要跟踪当前评级,可以修改协议并将监听器添加到FeedbackCell

protocol ClassFeedbackCellDelegate {
   func sharePressed(cell: FeedbackCell)
   func ratingDidChange(rating: Float)
}

class FeedbackCell: UITableViewCell {

  var delegate: ClassFeedbackCellDelegate?
  @IBOutlet weak var cosmosViewBelongingToFeedBackCell: CosmosView!
  @IBOutlet weak var submitButtonOutlet: UIButton!

  @IBAction func submitFeedBack(_ sender: Any) {
    delegate?.sharePressed(cell: self)
   }

  override func awakeFromNib() {
    super.awakeFromNib()
    cosmosViewBelongingToFeedBackCell.didFinishTouchingCosmos = {
      rating in
        self.delegate?.ratingDidChange(rating)
     }
  }
}

然后在DetailViewController

extension DetailViewController: ClassFeedbackCellDelegate {
  func ratingDidChange(rating: Float) {
    print(rating)
  }
}