Schaedel420
Schaedel420

Reputation: 175

UITableView + UISlider. Save Value for specific Index in Array

I have a survey tableView as shown in the Image. I would like to save the final value for each row at the specific index. "ajkpXc" for example stands for the first row, where User have to weigh up how important "ability job knowledge and productivity" is in comparison to "creativity". To use the information properly I have to store it to a specific index obviously.

All the solutions I try to achieve are not working out. I hope my goal is clear and some of you can find out a workable solution while I'm providing my code. :)

Btw.: I made an Array and a Dictionary, so you sort out what would be best to use in my purpose.

Thanks for your help!!!

class AHPRatingPriorityViewController: UIViewController {

// MARK: - Properties
    @IBOutlet weak var tableView: UITableView!
    @IBOutlet weak var saveButton: UIButton!
    @IBOutlet weak var errorLabel: UILabel!
    
    
    var userID:String?
    var currentValue:Int?
    private var ratingStorage = [Int](repeating: 0, count: 28)
    private static var rowsCount = 28
    let step : Float = 1

    
    let leftItems = ["Ability,\n Job Knowledge,\n Productivity",
                     "Ability, Job Knowledge, Productivity",
                     "Ability, Job Knowledge, Productivity",
                     "Ability, Job Knowledge, Productivity",
                     "Ability, Job Knowledge, Productivity",
                     "Ability, Job Knowledge, Productivity",
                     "Ability, Job Knowledge, Productivity",
                     "Creativity",
                     "Creativity",
                     "Creativity",
                     "Creativity",
                     "Creativity",
                     "Creativity",
                     "initiative, judgment, responsibility",
                     "initiative, judgment, responsibility",
                     "initiative, judgment, responsibility",
                     "initiative, judgment, responsibility",
                     "initiative, judgment, responsibility",
                     "quality of work, planning, accuracy",
                     "quality of work, planning, accuracy",
                     "quality of work, planning, accuracy",
                     "quality of work, planning, accuracy",
                     "Organization and Job Commitment",
                     "Organization and Job Commitment",
                     "Organization and Job Commitment",
                     "Organization and Supervisor Loyalty",
                     "Organization and Supervisor Loyalty",
                     "interpersonal relationships,\n attitude,\n communication skills,\n cooperation"]

    let rightItems = ["Creativity",
                      "Initiative, Judgment, Responsibility",
                      "Quality of work, planning, accuracy",
                      "Organization and Job Commitment",
                      "Organization and Supervisor Loyalty",
                      "interpersonal relationships, attitude, communication skills, cooperation",
                      "dependability, punctuality, honesty",
                      "initiative, judgment, responsibility",
                      "quality of work, planning, accuracy",
                      "Organization and Job Commitment",
                      "Organization and Supervisor Loyalty",
                      "interpersonal relationships, attitude communication skills, cooperation",
                      "dependability, punctuality, honesty",
                      "Quality of work, planning, accuracy",
                      "Organization and Job Commitment",
                      "Organization and Supervisor Loyalty",
                      "interpersonal relationships, attitude, communication skills, cooperation",
                      "dependability, punctuality, honesty",
                      "Organization and Job Commitment",
                      "Organization and Supervisor Loyalty",
                      "interpersonal relationships, attitude, communication skills, cooperation",
                      "dependability, punctuality, honesty",
                      "Organization and Supervisor Loyalty",
                      "interpersonal relationships, attitude, communication skills, cooperation",
                      "dependability, punctuality, honesty",
                      "interpersonal relationships, attitude, communication skills, cooperation",
                      "dependability, punctuality, honesty",
                      "dependability, punctuality, honesty"]

    
    override func viewDidLoad() {
        super.viewDidLoad()

        setUpElements()
               
               
        self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
               
        for i in 0..<AHPRatingPriorityViewController.rowsCount {
          ratingStorage[i] = Int(i) / 27 * 18
        }
        


    }
    
    // MARK: - Element Style
       func setUpElements() {
           
           
            // Hide the error label
            errorLabel.alpha = 0

       }
       
       
       func configureTableView() {
           // remove separators for empty cells
          tableView.tableFooterView = UIView()
           // remove separators from cells
           tableView.separatorStyle = .none
       }
       
       // Error Handling for save
       func showError(_ message:String) {

           errorLabel.text = message
           errorLabel.alpha = 1
          }
    
       
       
       func validateFields() -> String? {
           
           return nil
       }
              

@IBAction func saveRating(_ sender: Any) {
        
        
        let user = Auth.auth().currentUser
                  if  let user = user {

                      let uid = user.uid
                      userID = uid

                      // Validate the fields
                      let error = validateFields()

                      if error != nil {

                          // There is something wrong with the fields, show error message
                          print("an error occured")
                      } else {
                        
                        
                        var AHPPriorities: [String: Int] = ["ajkpXc": 0,"ajkpXijr": 0,"ajkpXqpa": 0,"ajkpXcoj": 0,"ajkpXlos": 0,"ajkpXiracc": 0, "ajkpXdph": 0,"cXijr": 0,"cXqpa": 0, "cXcoj": 0, "cXlos": 0, "cXiracc": 0,"cXdph": 0, "ijrXqpa": 0, "ijrXcoj": 0,"ijrXlos": 0,"ijrXiracc": 0,"ijrXdph": 0, "qpaXcoj": 0, "qpaXlos": 0, "qpaXiracc": 0, "qpaXdph": 0,"cojXlos": 0, "cojXiracc": 0,"cojXdph": 0, "losXiracc": 0, "losXdph": 0, "iraccXdph": 0]
                        
                        let ajkpXc = ratingStorage[0]
                        let ajkpXijr = ratingStorage[1]
                        let ajkpXqpa = ratingStorage[2]
                        let ajkpXcoj = ratingStorage[3]
                        let ajkpXlos = ratingStorage[4]
                        let ajkpXiracc = ratingStorage[5]
                        let ajkpXdph = ratingStorage[6]
                        let cXijr = ratingStorage[7]
                        let cXqpa = ratingStorage[8]
                        let cXcoj = ratingStorage[9]
                        let cXlos = ratingStorage[10]
                        let cXiracc = ratingStorage[11]
                        let cXdph = ratingStorage[12]
                        let ijrXqpa = ratingStorage[13]
                        let ijrXcoj = ratingStorage[14]
                        let ijrXlos = ratingStorage[15]
                        let ijrXiracc = ratingStorage[16]
                        let ijrXdph = ratingStorage[17]
                        let qpaXcoj = ratingStorage[18]
                        let qpaXlos = ratingStorage[19]
                        let qpaXiracc = ratingStorage[20]
                        let qpaXdph = ratingStorage[21]
                        let cojXlos = ratingStorage[22]
                        let cojXiracc = ratingStorage[23]
                        let cojXdph = ratingStorage[24]
                        let losXiracc = ratingStorage[25]
                        let losXdph = ratingStorage[26]
                        let iraccXdph = ratingStorage[27]

                        GLajkpXc = ajkpXc
                        GLajkpXijr = ajkpXijr
                        GLajkpXqpa = ajkpXqpa
                        GLajkpXcoj = ajkpXcoj
                        GLajkpXlos = ajkpXlos
                        GLajkpXiracc = ajkpXiracc
                        GLajkpXdph = ajkpXdph
                        GLcXijr = cXijr
                        GLcXqpa = cXqpa
                        GLcXcoj = cXcoj
                        GLcXlos = cXlos
                        GLcXiracc = cXiracc
                        GLcXdph =  cXdph
                        GLijrXqpa = ijrXqpa
                        GLijrXcoj = ijrXcoj
                        GLijrXlos = ijrXlos
                        GLijrXiracc =  ijrXiracc
                        GLijrXdph =  ijrXdph
                        GLqpaXcoj = qpaXcoj
                        GLqpaXlos = qpaXlos
                        GLqpaXiracc =   qpaXiracc
                        GLqpaXdph = qpaXdph
                        GLcojXlos = cojXlos
                        GLcojXiracc = cojXiracc
                        GLcojXdph = cojXdph
                        GLlosXiracc =   losXiracc
                        GLlosXdph = losXdph
                        GLiraccXdph = iraccXdph
                        
                        
                        
                        guard let firUser = Auth.auth().currentUser else {return}
                        UserService.ahpPref(firUser) { (user) in
                                          
                                      guard let user = user else {
                                          // handle error
                                          return
                                      }
   }
//
//} else { print("Please log out and log in again")}
        
        
//        for item in itemsForRating {
//            print("Thats the item:  \(item)")
//        }
//
        for rating in ratingStorage {
            print("Thats the rating: \(rating)")
        }
//
//        print("Thats the member ID: ", memberID)
        
}
 

        }
    }

}
// MARK: - Extension

extension AHPRatingPriorityViewController: UITableViewDataSource {


    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return leftItems.count

    }


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

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {


       }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        
        let ratingItemLeft = leftItems[indexPath.row]
        let ratingItemRight = rightItems[indexPath.row]
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "RatingPriorityTableViewCell", for: indexPath) as! RatingPriorityTableViewCell

        
//        cell.update()

        cell.leftItems.text = ratingItemLeft
        cell.rightItems.text = ratingItemRight
        


        cell.itemSlider.tag = indexPath.row
        cell.itemSlider.addTarget(self, action: #selector(AHPRatingPriorityViewController.sliderChange(sender:)), for: .valueChanged)
            
        
    
        return cell

        
    }
    
    @objc func sliderChange(sender: UISlider) {
          
           

           // Snap the slider
           let roundedStepValue = round(sender.value / step) * step
           sender.value = roundedStepValue
           
           
           // Get the sliders value
              currentValue = Int(sender.value)
               let row = sender.tag               // get slider's row in table

       
    
        for i in 0..<leftItems.count {
            ratingStorage.insert(currentValue!, at: i)
              
            }
           
           print(#function)
           
//        for rating in ratingStorage {
//            print("Slider has a value of \(currentValue)")

//        }
    }
        
    
}

// MARK: - UITableViewDelegate

extension AHPRatingPriorityViewController: UITableViewDelegate {

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
     return 196
    }
}
import Foundation
import UIKit
import FirebaseDatabase.FIRDataSnapshot

class AHPPriority {
    
    var key: String?
   
    let ajkpXc: Int
    let ajkpXijr: Int
    let ajkpXqpa: Int
    let ajkpXcoj: Int
    let ajkpXlos: Int
    let ajkpXiracc: Int
    let ajkpXdph: Int
    let cXijr: Int
    let cXqpa: Int
    let cXcoj: Int
    let cXlos: Int
    let cXiracc: Int
    let cXdph: Int
    let ijrXqpa: Int
    let ijrXcoj: Int
    let ijrXlos: Int
    let ijrXiracc: Int
    let ijrXdph: Int
    let qpaXcoj: Int
    let qpaXlos: Int
    let qpaXiracc: Int
    let qpaXdph: Int
    let cojXlos: Int
    let cojXiracc: Int
    let cojXdph: Int
    let losXiracc: Int
    let losXdph: Int
    let iraccXdph: Int

    
    init(ajkpXc: Int, ajkpXijr: Int, ajkpXqpa: Int, ajkpXcoj: Int, ajkpXlos: Int, ajkpXiracc: Int, ajkpXdph: Int, cXijr: Int, cXqpa: Int, cXcoj: Int, cXlos: Int, cXiracc: Int, cXdph: Int, ijrXqpa: Int, ijrXcoj: Int, ijrXlos: Int, ijrXiracc: Int, ijrXdph: Int, qpaXcoj: Int,  qpaXlos: Int,   qpaXiracc: Int,  qpaXdph: Int, cojXlos: Int, cojXiracc: Int, cojXdph: Int, losXiracc: Int, losXdph: Int, iraccXdph: Int) {
        
 
                self.ajkpXc = ajkpXc
                self.ajkpXijr = ajkpXijr
                self.ajkpXqpa = ajkpXqpa
                self.ajkpXcoj = ajkpXcoj
                self.ajkpXlos = ajkpXlos
                self.ajkpXiracc = ajkpXiracc
                self.ajkpXdph = ajkpXdph
                self.cXijr = cXijr
                self.cXqpa = cXqpa
                self.cXcoj = cXcoj
                self.cXlos = cXlos
                self.cXiracc = cXiracc
                self.cXdph =  cXdph
                self.ijrXqpa = ijrXqpa
                self.ijrXcoj = ijrXcoj
                self.ijrXlos = ijrXlos
                self.ijrXiracc =  ijrXiracc
                self.ijrXdph =  ijrXdph
                self.qpaXcoj = qpaXcoj
                self.qpaXlos = qpaXlos
                self.qpaXiracc =   qpaXiracc
                self.qpaXdph = qpaXdph
                self.cojXlos = cojXlos
                self.cojXiracc = cojXiracc
                self.cojXdph = cojXdph
                self.losXiracc =   losXiracc
                self.losXdph = losXdph
                self.iraccXdph = iraccXdph
        
        
    }

    var dictValue: [String: Any] {
    let createdAgo = kSecAttrCreationDate
    
    return [
        "created_ago": createdAgo,
        "ajkpXc": ajkpXc,
        "ajkpXijr ": ajkpXijr,
        "ajkpXqpa": ajkpXqpa,
        "ajkpXcoj": ajkpXcoj,
        "ajkpXlos ": ajkpXlos,
        "ajkpXiracc ": ajkpXiracc,
        "ajkpXdph": ajkpXdph,
        "cXijr": cXijr,
        "cXqpa": cXqpa,
        "cXcoj": cXcoj,
        "cXlos": cXlos,
        "cXiracc": cXiracc,
        "cXdph": cXdph,
        "ijrXqpa": ijrXqpa,
        "ijrXcoj": ijrXcoj,
        "ijrXlos": ijrXlos,
        "ijrXiracc": ijrXiracc,
        "ijrXdph": ijrXdph,
        "qpaXcoj": qpaXcoj,
        "qpaXlos": qpaXlos,
        "qpaXiracc": qpaXiracc,
        "qpaXdph": qpaXdph,
        "cojXlos": cojXlos,
        "cojXiracc": cojXiracc,
        "cojXdph": cojXdph,
        "losXiracc": losXiracc,
        "losXdph": losXdph,
        "iraccXdph": iraccXdph ]
     
}
    
    init?(snapshot: DataSnapshot) {
    guard let dict = snapshot.value as? [String: Any],
        
    let ajkpXc = dict["ajkpXc"] as? Int,
    let ajkpXijr = dict["ajkpXijr"] as? Int,
    let ajkpXqpa = dict["ajkpXqpa"] as? Int,
    let ajkpXcoj = dict["ajkpXcoj"] as? Int,
    let ajkpXlos = dict["ajkpXlos"] as? Int,
    let ajkpXiracc = dict["ajkpXiracc"] as? Int,
    let ajkpXdph = dict["ajkpXdph"] as? Int,
    let cXijr = dict["cXijr"] as? Int,
    let cXqpa = dict["cXqpa"] as? Int,
    let cXcoj = dict["cXcoj"] as? Int,
    let cXlos = dict["cXlos"] as? Int,
    let cXiracc = dict["cXiracc"] as? Int,
    let cXdph = dict["cXdph"] as? Int,
    let ijrXqpa = dict["ijrXqpa"] as? Int,
    let ijrXcoj = dict["ijrXcoj"] as? Int,
    let ijrXlos = dict["ijrXlos"] as? Int,
    let ijrXiracc = dict["ijrXiracc"] as? Int,
    let ijrXdph = dict["ijrXdph"] as? Int,
    let qpaXcoj = dict["qpaXcoj"] as? Int,
    let qpaXlos = dict["qpaXlos"] as? Int,
    let qpaXiracc = dict["qpaXiracc"] as? Int,
    let qpaXdph = dict["qpaXdph"] as? Int,
    let cojXlos = dict["cojXlos"] as? Int,
    let cojXiracc = dict["cojXiracc"] as? Int,
    let cojXdph = dict["cojXdph"] as? Int,
    let losXiracc = dict["losXiracc"] as? Int,
    let losXdph = dict["losXdph"] as? Int,
    let iraccXdph = dict["iraccXdph"] as? Int

     else {return nil}
        
        
        self.ajkpXc = ajkpXc
        self.ajkpXijr = ajkpXijr
        self.ajkpXqpa = ajkpXqpa
        self.ajkpXcoj = ajkpXcoj
        self.ajkpXlos = ajkpXlos
        self.ajkpXiracc = ajkpXiracc
        self.ajkpXdph = ajkpXdph
        self.cXijr = cXijr
        self.cXqpa = cXqpa
        self.cXcoj = cXcoj
        self.cXlos = cXlos
        self.cXiracc = cXiracc
        self.cXdph =  cXdph
        self.ijrXqpa = ijrXqpa
        self.ijrXcoj = ijrXcoj
        self.ijrXlos = ijrXlos
        self.ijrXiracc =  ijrXiracc
        self.ijrXdph =  ijrXdph
        self.qpaXcoj = qpaXcoj
        self.qpaXlos = qpaXlos
        self.qpaXiracc =   qpaXiracc
        self.qpaXdph = qpaXdph
        self.cojXlos = cojXlos
        self.cojXiracc = cojXiracc
        self.cojXdph = cojXdph
        self.losXiracc =   losXiracc
        self.losXdph = losXdph
        self.iraccXdph = iraccXdph
        }

}
struct UserService {
    
    static let dateFormatter = ISO8601DateFormatter()
 static func ahpPref(_ firUser: FIRUser, completion: @escaping (User?) -> Void) {
        
        let timestamp = dateFormatter.string(from: Date())
        let ref = Database.database().reference().child("Leader").child(firUser.uid).child("AHPPriority").child(timestamp)
        
        let ahpPriorities = [
        "ajkpXc": GLajkpXc,
        "ajkpXijr ": GLajkpXijr,
        "ajkpXqpa": GLajkpXqpa,
        "ajkpXcoj": GLajkpXcoj,
        "ajkpXlos ": GLajkpXlos,
        "ajkpXiracc ": GLajkpXiracc,
        "ajkpXdph": GLajkpXdph,
        "cXijr": GLcXijr,
        "cXqpa": GLcXqpa,
        "cXcoj": GLcXcoj,
        "cXlos": GLcXlos,
        "cXiracc": GLcXiracc,
        "cXdph": GLcXdph,
        "ijrXqpa": GLijrXqpa,
        "ijrXcoj": GLijrXcoj,
        "ijrXlos": GLijrXlos,
        "ijrXiracc": GLijrXiracc,
        "ijrXdph": GLijrXdph,
        "qpaXcoj": GLqpaXcoj,
        "qpaXlos": GLqpaXlos,
        "qpaXiracc": GLqpaXiracc,
        "qpaXdph": GLqpaXdph,
        "cojXlos": GLcojXlos,
        "cojXiracc": GLcojXiracc,
        "cojXdph": GLcojXdph,
        "losXiracc": GLlosXiracc,
        "losXdph": GLlosXdph,
        "iraccXdph": GLiraccXdph ]
        
       ref.setValue(ahpPriorities) { (error, ref) in
        if let error = error {
                     assertionFailure(error.localizedDescription)
                     return completion(nil)
                 }

                 ref.observeSingleEvent(of: .value, with: { (snapshot) in
                     let user = User(snapshot: snapshot)
                     completion(user)
                 })
             }
         }

        
}

Upvotes: 0

Views: 65

Answers (1)

Muhammad Ali
Muhammad Ali

Reputation: 915

You have written to much code which I don't think we need in our case. First of all, replace ratingStorage array and with a dictionary with string as key and int as value

var ratingStorage = [String: Int]()

you can initialize it with your rows as key and the default values of slider if it is not changed

inside the function 'sliderChange' you are doing the mistake simply replace the code

for i in 0..<leftItems.count {
        ratingStorage.insert(currentValue!, at: i)
          
        }

with

ratingStorage[row] = currentValue

inside saveRating function remove all the useLess Code and Simply print the ratingStorage dictionary which will contain the row as key and the "slider value" as it Value. That's it hopefully it will provide a solution.

Upvotes: 1

Related Questions