Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // attendanceViewController.swift
- // studentHandbook
- //
- // Created by Rengar Tsoi on 11/4/2017.
- // Copyright © 2017年 Rengar Tsoi. All rights reserved.
- //
- import UIKit
- import Alamofire
- import SwiftyJSON
- class attendanceViewController: UIViewController, UITableViewDelegate, UITableViewDataSource{
- @IBOutlet weak var table: UITableView!
- var studArray: [attendance]? = []
- override func viewDidLoad() {
- super.viewDidLoad()
- //table.dataSource = self
- //table.delegate = self
- fetchstDetails()
- }
- func fetchstDetails(){
- let username = "parentX"
- let password = "pw"
- let urlString = "https://lenchan139.org/myWorks/fyp/android/attendDetails.php?username=" + username + "&password=" + password
- let urlRequest = URLRequest(url:URL(string: urlString)!)
- let task = URLSession.shared.dataTask(with: urlRequest) { (data,response,error) in
- if error != nil{
- print(error!)
- return
- }
- self.studArray = [attendance]()
- do {
- let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as! [String:AnyObject]
- if let attJson = json["studArray"] as? [[String : AnyObject]] {
- for stDetails in attJson{
- let stdAtt = attendance()
- 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{
- for i in 0...(stdDate.count)-1{
- let row = stdDate[i] as! NSDictionary
- let date = row["attend_date"] as! String
- stdAtt.stdId = stdId
- stdAtt.stdClass = stdClass
- stdAtt.attDate = date
- stdAtt.stdName = stdName
- print("date:",date)
- //
- // for dateArray in stdDate{
- // stdAtt.attDate = dateArray as? [String: AnyObject]
- // let date = dateArray["attend_date"] as! String
- // print(date)
- // }
- //stdAtt.attDate = stdDate as! [[String : AnyObject]]
- //print(stdAtt.attDate!)
- self.studArray?.append(stdAtt)
- }
- }
- }
- }
- DispatchQueue.main.sync {
- self.table.reloadData()
- }
- } catch let error {
- print(error)
- }
- }
- task.resume()
- }
- func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
- let cell = tableView.dequeueReusableCell(withIdentifier: "attCell", for: indexPath) as! TableViewCell
- // for (index, value) in (self.studArray?[indexPath.item].attDate?.enumerated())! {
- // print(value["attend_date"])
- // let string = value["attend_date"] as? String
- // cell.attDate.text = "" + "\(String(describing: string))"
- //// cell.attDate.text = "" + value as? String
- // }
- cell.attDate.text = self.studArray?[indexPath.item].attDate
- cell.stdName.text = self.studArray?[indexPath.item].stdName
- cell.stdClass.text = self.studArray?[indexPath.item].stdClass
- return cell
- }
- func numberOfSections(in tableView: UITableView) -> Int {
- return 1
- }
- func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
- return studArray!.count
- }
- @IBAction func logoutBtn(_ sender: Any) {
- UserDefaults.standard.set(nil, forKey: "username");
- UserDefaults.standard.set(nil, forKey: "password");
- UserDefaults.standard.synchronize()
- let username = UserDefaults.standard.string(forKey: "username");
- let password = UserDefaults.standard.string(forKey: "password");
- if(username == nil && password == nil){
- performSegue(withIdentifier: "loginPage", sender: self)
- }
- }
- /*
- // MARK: - Navigation
- // In a storyboard-based application, you will often want to do a little preparation before navigation
- override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
- // Get the new view controller using segue.destinationViewController.
- // Pass the selected object to the new view controller.
- }
- */
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement