如何保存索引和评级值?

时间:2017-10-24 09:22:52

标签: ios swift

这里我使用HCSStarRatingview进行评级,这里我已经在表格视图中实现了它,这样我就需要获得所选的特定评级值,并且需要根据其索引进行保存,并且还应该需要稍后也更改保存的值并且需要在所选索引中插入任何人都可以帮我实现这个吗?

这是我的代码

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if indexPath.section == 0 {
            let cell = tableView.dequeueReusableCell(withIdentifier: "ratingCell", for: indexPath) as! ratingCell
            return cell
        }
        else if indexPath.section == 1 {
            let cell = tableView.dequeueReusableCell(withIdentifier: "reviewCell", for: indexPath) as! AddaReviewTableViewCell
            let arr = reviewModel[indexPath.row]
            cell.reviewTypeLabel.text = arr.ratingCode
            cell.starRatingControl.tag = indexPath.row
            cell.starRatingControl.addTarget(self, action: #selector(RatingControlAction(_:)), for: .touchUpInside)
            return cell
        }
        else{
            let cell = tableView.dequeueReusableCell(withIdentifier: "reviewInputCell", for: indexPath) as! reviewInputCell
            nickName = cell.nameTextField.text
            summary = cell.summaryTextField.text
            review = cell.reviewTextView.text
            cell.submitReviewButton.addTarget(self, action: #selector(submitReviewAction(_:)), for: .touchUpInside)
            return cell
        }
    }
    func isValidFirstname(testStr:String) -> Bool {
        let firstnameRegEx = "^[a-zA-Z]+$"
        let firstnameTest = NSPredicate(format:"SELF MATCHES %@", firstnameRegEx)
        let result = firstnameTest.evaluate(with: testStr)
        return result
    }
    func RatingControlAction(_ sender:Any){
        let buttonPosition = (sender as AnyObject).convert(CGPoint(), to: tableDetails)
        let index = tableDetails.indexPathForRow(at: buttonPosition)
        print(String(format: "Changed rating to %.1f", (sender as AnyObject as! CVarArg)))
    }

此处显示的屏幕截图

enter image description here

1 个答案:

答案 0 :(得分:0)

为什么不创建一个可以容纳两个值的结构

声明一个结构

struct Struct_Row_Rating {
var row: String
var rating: Int

    init(row: String , rating: Int ) {
    self.row = row
    self.rating = rating

}}

声明一个数组

     var arrayAdaptor  = [Struct_Row_Rating]()

像这样追加数组

  arrayAdaptor.append(Struct_Row_Rating(row: IndexPath.row, rating: 3)

您可以通过

检索值
var index1 = arrayAdaptor[1].rating // use dynamic value here

More on struct