Advertisement
Guest User

Untitled

a guest
May 16th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 5.21 KB | None | 0 0
  1. //
  2. //  attendanceViewController.swift
  3. //  studentHandbook
  4. //
  5. //  Created by Rengar Tsoi on 11/4/2017.
  6. //  Copyright © 2017年 Rengar Tsoi. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10. import  Alamofire
  11. import SwiftyJSON
  12.  
  13. class attendanceViewController: UIViewController, UITableViewDelegate, UITableViewDataSource{
  14.    
  15.     @IBOutlet weak var table: UITableView!
  16.     var studArray: [attendance]? = []
  17.    
  18.  
  19.     override func viewDidLoad() {
  20.         super.viewDidLoad()
  21.        
  22.         //table.dataSource = self
  23.         //table.delegate = self
  24.        
  25.        
  26.        
  27.        
  28.         fetchstDetails()
  29.        
  30.     }
  31.    
  32.  
  33.  
  34.    
  35.    
  36.    
  37.     func fetchstDetails(){
  38.        
  39.         let username = "parentX"
  40.         let password = "pw"
  41.        
  42.        
  43.         let urlString = "https://lenchan139.org/myWorks/fyp/android/attendDetails.php?username=" + username + "&password=" + password
  44.        
  45.         let urlRequest = URLRequest(url:URL(string: urlString)!)
  46.        
  47.        
  48.        
  49.        
  50.  
  51.        
  52.         let task = URLSession.shared.dataTask(with: urlRequest) { (data,response,error) in
  53.            
  54.             if error != nil{
  55.            
  56.                 print(error!)
  57.                 return
  58.            
  59.             }
  60.             self.studArray = [attendance]()
  61.             do {
  62.                
  63.                 let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as! [String:AnyObject]
  64.                            
  65.                            
  66.                 if let attJson = json["studArray"] as? [[String : AnyObject]] {
  67.                                    
  68.                                    
  69.                     for stDetails in attJson{
  70.                        
  71.                         let stdAtt = attendance()
  72.                         if let stdId = stDetails["student_id"] as? String, let stdClass = stDetails["student_class"] as? String,let stdName = stDetails["student_name"] as? String, let stdDate = stDetails["student_attend"] as? NSArray{
  73.                            
  74.                             for i in 0...(stdDate.count)-1{
  75.                                 let row = stdDate[i] as! NSDictionary
  76.                                 let date = row["attend_date"] as! String
  77.                                 stdAtt.stdId = stdId
  78.                                 stdAtt.stdClass = stdClass
  79.                                 stdAtt.attDate = date
  80.                                 stdAtt.stdName = stdName
  81.                                
  82.                                 print("date:",date)
  83.                                                    
  84. //                                                 
  85. //                                                  for dateArray in stdDate{
  86. //                                                      stdAtt.attDate = dateArray as? [String: AnyObject]
  87. //                                                      let date = dateArray["attend_date"] as! String
  88. //                                                      print(date)
  89. //                                                  }
  90.                                                     //stdAtt.attDate = stdDate as! [[String : AnyObject]]
  91.                                                    
  92.                                                     //print(stdAtt.attDate!)
  93.                                     self.studArray?.append(stdAtt)         
  94.                             }
  95.                            
  96.                         }
  97.                        
  98.                     }
  99.                 }
  100.                
  101.                 DispatchQueue.main.sync {
  102.                     self.table.reloadData()
  103.                 }
  104.                
  105.             } catch let error {
  106.                 print(error)
  107.             }
  108.     }
  109.         task.resume()
  110.     }
  111.    
  112.     func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  113.         let cell = tableView.dequeueReusableCell(withIdentifier: "attCell", for: indexPath) as! TableViewCell
  114.            
  115. //          for (index, value)  in (self.studArray?[indexPath.item].attDate?.enumerated())! {
  116. //              print(value["attend_date"])
  117. //              let string = value["attend_date"] as? String
  118. //              cell.attDate.text = "" + "\(String(describing: string))"
  119. ////                cell.attDate.text = "" + value as? String
  120. //          }
  121.         cell.attDate.text = self.studArray?[indexPath.item].attDate
  122.         cell.stdName.text = self.studArray?[indexPath.item].stdName
  123.         cell.stdClass.text = self.studArray?[indexPath.item].stdClass
  124.  
  125.         return cell
  126.     }
  127.    
  128.     func numberOfSections(in tableView: UITableView) -> Int {
  129.         return 1
  130.    
  131.     }
  132.     func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  133.         return studArray!.count
  134.        
  135.     }
  136.  
  137.    
  138.    
  139.    
  140.     @IBAction func logoutBtn(_ sender: Any) {
  141.         UserDefaults.standard.set(nil, forKey: "username");
  142.         UserDefaults.standard.set(nil, forKey: "password");
  143.         UserDefaults.standard.synchronize()
  144.        
  145.         let username = UserDefaults.standard.string(forKey: "username");
  146.         let password = UserDefaults.standard.string(forKey: "password");
  147.        
  148.         if(username == nil && password == nil){
  149.            
  150.             performSegue(withIdentifier: "loginPage", sender: self)
  151.            
  152.         }
  153.     }
  154.  
  155.    
  156.    
  157.    
  158.    
  159.  
  160.     /*
  161.     // MARK: - Navigation
  162.  
  163.     // In a storyboard-based application, you will often want to do a little preparation before navigation
  164.     override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  165.         // Get the new view controller using segue.destinationViewController.
  166.         // Pass the selected object to the new view controller.
  167.     }
  168.     */
  169.  
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement