Advertisement
cowboy1222

Untitled

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