Skip to content

Instantly share code, notes, and snippets.

@punkyoon
Created November 1, 2017 05:57
Show Gist options
  • Select an option

  • Save punkyoon/4fab6a23ce982e58759f4fd82756d79c to your computer and use it in GitHub Desktop.

Select an option

Save punkyoon/4fab6a23ce982e58759f4fd82756d79c to your computer and use it in GitHub Desktop.
import UIKit
import Foundation
import Alamofire
class MyFriendsController: UITableViewController {
var refresher: UIRefreshControl!
var myFriendsInfo: [PersonInfo] = []
@IBOutlet weak var navigationBar: UINavigationItem!
var myActivityIndicator = UIActivityIndicatorView(activityIndicatorStyle:UIActivityIndicatorViewStyle.gray)
override func viewDidLoad() {
super.viewDidLoad()
refresher = UIRefreshControl()
refresher.addTarget(self, action: #selector(MyFriendsController.populate), for: .valueChanged)
tableView.addSubview(refresher)
self.getFriendsList()
self.myActivityIndicator.hidesWhenStopped = true
view.addSubview(self.myActivityIndicator)
let barButtonItem = UIBarButtonItem(customView: self.myActivityIndicator)
self.navigationBar.rightBarButtonItem = barButtonItem
}
func addRefreshHeader() {
self.myActivityIndicator.startAnimating()
}
func removeRefreshHeader() {
self.myActivityIndicator.stopAnimating()
//self.navigationBar.rightBarButtonItem = nil
}
func getFriendsList() {
let result = Alamofire.request("https://randomuser.me/api/?results=20&inc=name,picture,nat,cell,email,id")
result.responseJSON { res in
let jsonData = res.result.value! as! NSDictionary
let results = jsonData["results"] as! NSArray
for case let result as NSDictionary in results {
let name_info = result["name"] as! NSDictionary
let person: PersonInfo = PersonInfo(
first_name: name_info["first"] as! String,
last_name: name_info["last"] as! String,
gender: name_info["title"] as! String,
email: result["email"] as! String,
phone: result["cell"] as! String,
location: result["nat"] as! String
)
self.myFriendsInfo.append(person)
}
}
}
@objc func populate() {
self.getFriendsList()
refresher.endRefreshing()
tableView.reloadData()
}
// table view reload!!
@IBAction func reloadFriendList(_ sender: Any) {
self.addRefreshHeader()
self.getFriendsList()
self.removeRefreshHeader()
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let newView = segue.destination as! DetailViewController
newView.nameVal = "test"
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
let rowCount: Int = self.myFriendsInfo.count
return rowCount
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "myfriendsCell", for: indexPath)
let person = self.myFriendsInfo[indexPath.row]
cell.textLabel!.text = person.first_name + " " + person.last_name
cell.detailTextLabel!.text = person.email
return cell
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment