Advertisement
cowboy1222

Untitled

Apr 20th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 4.30 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.     func get_data(_ link:String)
  65.     {
  66.         let url:URL = URL(string: link)!
  67.         let session = URLSession.shared
  68.        
  69.         let request = URLRequest(url: url)
  70.        
  71.         let task = session.dataTask(with: request, completionHandler: {
  72.             (data, response, error) in
  73.            
  74.             self.extract_data(data)
  75.            
  76.         })
  77.        
  78.         task.resume()
  79.     }
  80.    
  81.    
  82.     func extract_data(_ data:Data?)
  83.     {
  84.         let json:Any?
  85.        
  86.         if(data == nil)
  87.         {
  88.             return
  89.         }
  90.        
  91.         do{
  92.             json = try JSONSerialization.jsonObject(with: data!, options: [])
  93.         }
  94.         catch
  95.         {
  96.             return
  97.         }
  98.        
  99.         guard let data_array = json as? NSArray else
  100.         {
  101.             return
  102.         }
  103.        
  104.        
  105.         for i in 0 ..< data_array.count
  106.         {
  107.             if let data_object = data_array[i] as? NSDictionary
  108.             {
  109.                 if let data_code = data_object["attend_date"] as? String
  110.                    
  111.                 {
  112.                     list.append(MyStruct(data_code))
  113.                 }
  114.                
  115.             }
  116.         }
  117.        
  118.        
  119.         refresh_now()
  120.        
  121.        
  122.     }
  123.    
  124.     func refresh_now()
  125.     {
  126.         DispatchQueue.main.async(
  127.             execute:
  128.             {
  129.                 self.table.reloadData()
  130.                
  131.         })
  132.     }
  133.    
  134.    
  135.    
  136.     public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
  137.     {
  138.        
  139.         return list.count
  140.     }
  141.    
  142.     public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
  143.     {
  144.         let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
  145.        
  146.        
  147.         cell.textLabel?.text = list[indexPath.row].attend
  148.        
  149.        
  150.         return cell
  151.     }
  152.    
  153.  
  154.    
  155.    
  156.    
  157.     @IBAction func logoutBtn(_ sender: Any) {
  158.         UserDefaults.standard.set(nil, forKey: "username");
  159.         UserDefaults.standard.set(nil, forKey: "password");
  160.         UserDefaults.standard.synchronize()
  161.        
  162.         let username = UserDefaults.standard.string(forKey: "username");
  163.         let password = UserDefaults.standard.string(forKey: "password");
  164.        
  165.         if(username == nil && password == nil){
  166.            
  167.             performSegue(withIdentifier: "loginPage", sender: self)
  168.            
  169.         }
  170.     }
  171.  
  172.    
  173.    
  174.    
  175.    
  176.  
  177.     /*
  178.     // MARK: - Navigation
  179.  
  180.     // In a storyboard-based application, you will often want to do a little preparation before navigation
  181.     override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  182.         // Get the new view controller using segue.destinationViewController.
  183.         // Pass the selected object to the new view controller.
  184.     }
  185.     */
  186.  
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement