Advertisement
cowboy1222

Untitled

Apr 21st, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 4.68 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.    
  15.  
  16.     override func viewDidLoad() {
  17.         super.viewDidLoad()
  18.        
  19.         table.dataSource = self
  20.         table.delegate = self
  21.        
  22.         let username = "parentX"
  23.         let password = "pw"
  24.  
  25.        
  26.         let urlString = "https://lenchan139.org/myWorks/fyp/android/attendDetails.php?username=" + username + "&password=" + password;
  27.        
  28.         //get_data(urlString)
  29.        
  30.         print(urlString)
  31.        
  32.         //print(get_data(urlString))
  33.  
  34.         // Do any additional setup after loading the view.
  35.     }
  36.  
  37.     override func didReceiveMemoryWarning() {
  38.         super.didReceiveMemoryWarning()
  39.         // Dispose of any resources that can be recreated.
  40.     }
  41.    
  42.     override func viewWillAppear(_ animated: Bool) {
  43.         super.viewWillAppear(animated)
  44.        
  45.     }
  46.    
  47.     var list:[MyStruct] = [MyStruct]()
  48.    
  49.     struct MyStruct
  50.     {
  51.         var attend = ""
  52.        
  53.        
  54.         init(_ attend:String)
  55.         {
  56.             self.attend = attend
  57.            
  58.         }
  59.     }
  60.    
  61.    
  62.    
  63.    
  64.    
  65.    
  66.     func get_data(_ link:String)
  67.     {
  68.         let url:URL = URL(string: link)!
  69.         let session = URLSession.shared
  70.        
  71.         let request = URLRequest(url: url)
  72.        
  73.         let task = session.dataTask(with: request, completionHandler: {
  74.             (data, response, error) in
  75.            
  76.             self.extract_data(data)
  77.            
  78.         })
  79.        
  80.         task.resume()
  81.     }
  82.    
  83.    
  84.     func extract_data(_ data:Data?)
  85.     {
  86.        
  87.        
  88.         if(data == nil)
  89.         {
  90.             return
  91.         }
  92.        
  93.         do{
  94.             let parsedData = try JSONSerialization.jsonObject(with: data!, options: []) as! [String:AnyObject]
  95.             guard let dictStudArray = parsedData as! NSArray else
  96.             {
  97.                 return
  98.             }
  99.            
  100.            
  101.             for i in 0 ..< dictStudArray.count
  102.             {
  103.                
  104.                 let dictStudAttend = parsedData["attend_date"] as! NSArray;
  105.                 if let data_object = dictStudAttend[i] as? NSDictionary
  106.                 {
  107.                 //if let data_object = data_array[i] as? NSDictionary
  108.                    
  109.                 print(dictStudAttend, i)
  110.                     if let data_code = data_object["attend_date"] as? String
  111.                        
  112.                        
  113.                     {
  114.                         list.append(MyStruct(data_code))
  115.                     }
  116.                    
  117.                 }
  118.             }
  119.         }
  120.         catch
  121.         {
  122.             return
  123.         }
  124.        
  125.        
  126.        
  127.        
  128.         refresh_now()
  129.        
  130.        
  131.     }
  132.  
  133.    
  134.     func refresh_now()
  135.     {
  136.         DispatchQueue.main.async(
  137.             execute:
  138.             {
  139.                 self.table.reloadData()
  140.                
  141.         })
  142.     }
  143.    
  144.    
  145.    
  146.     public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
  147.     {
  148.        
  149.         return list.count
  150.     }
  151.    
  152.     public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
  153.     {
  154.         let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
  155.        
  156.        
  157.         cell.textLabel?.text = list[indexPath.row].attend
  158.        
  159.        
  160.         return cell
  161.     }
  162.    
  163.  
  164.    
  165.    
  166.    
  167.     @IBAction func logoutBtn(_ sender: Any) {
  168.         UserDefaults.standard.set(nil, forKey: "username");
  169.         UserDefaults.standard.set(nil, forKey: "password");
  170.         UserDefaults.standard.synchronize()
  171.        
  172.         let username = UserDefaults.standard.string(forKey: "username");
  173.         let password = UserDefaults.standard.string(forKey: "password");
  174.        
  175.         if(username == nil && password == nil){
  176.            
  177.             performSegue(withIdentifier: "loginPage", sender: self)
  178.            
  179.         }
  180.     }
  181.  
  182.    
  183.    
  184.    
  185.    
  186.  
  187.     /*
  188.     // MARK: - Navigation
  189.  
  190.     // In a storyboard-based application, you will often want to do a little preparation before navigation
  191.     override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  192.         // Get the new view controller using segue.destinationViewController.
  193.         // Pass the selected object to the new view controller.
  194.     }
  195.     */
  196.  
  197. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement