If I can get help with that. I did change the URL for security reasons, but assume that I am getting correct response as JSON format.
@IBOutlet weak var leaderBoardTableView: UITableView!
let url = URL(string: "http://sak51.tnyre.com/getSampleData")
var numRows = 0;
var data: JSON = [];
func getApiData() {
let request = URLRequest(url: url!)
do {
URLSession.shared.dataTask(with: request) { data, response, error in
let json = JSON(data)
self.data = json["data"]
self.numRows = json["data"].count
DispatchQueue.global(qos: .background).async {
DispatchQueue.main.async {
self.leaderBoardTableView.reloadData()
}
}
print(json)
}.resume()
} catch {
print(error)
}
}
override func viewDidLoad() {
super.viewDidLoad()
getApiData()
leaderBoardTableView.delegate = self
leaderBoardTableView.dataSource = self
print(currentUser.userName)
self.tableView.backgroundColor = UIColor.black;
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
print("numofRoesInsec called")
return self.numRows
}
override func tableView(_ tableView: UITableView,
titleForHeaderInSection section: Int) -> String {
return "Leaderboard"
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
print("table view loading")
let cell = tableView.dequeueReusableCell(withIdentifier: "LeaderboardTableViewCell", for: indexPath) as! LeaderboardTableViewCell
cell.backgroundColor = UIColor.black
cell.name.textColor = UIColor.white
cell.rank.textColor = UIColor.white
cell.score.textColor = UIColor.white
cell.name.text = "\(self.data[indexPath.row]["username"])"
cell.score.text = "\(self.data[indexPath.row]["score"])"
cell.rank.text = "\(self.data[indexPath.row]["rank"])"
cell.name.textAlignment = .justified
cell.rank.textAlignment = .justified
cell.score.textAlignment = .justified
if (self.data[indexPath.row]["username"] as? String != "sak2") {
cell.selectionStyle = .none
} else {
cell.selectionStyle = .default
}
if (cell.name.text == "sak2") {
cell.selectionStyle = .default
cell.setSelected(true, animated: true)
}
return cell
}
override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int){
view.tintColor = UIColor.black
let header = view as! UITableViewHeaderFooterView
header.textLabel?.adjustsFontSizeToFitWidth = true
header.textLabel?.textColor = UIColor.green
}
/*
// Override to support conditional editing of the table view.
override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
// Return false if you do not want the specified item to be editable.
return true
}
*/
/*
// Override to support editing the table view.
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
// Delete the row from the data source
tableView.deleteRows(at: [indexPath], with: .fade)
} else if editingStyle == .insert {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
*/
/*
// Override to support rearranging the table view.
override func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath) {
}
*/
/*
// Override to support conditional rearranging of the table view.
override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
// Return false if you do not want the item to be re-orderable.
return true
}
*/
/*
// 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.destination.
// Pass the selected object to the new view controller.
}
*/
}
this is a cell class:
class LeaderboardTableViewCell: UITableViewCell {
@IBOutlet weak var rank: UILabel!
@IBOutlet weak var score: UILabel!
@IBOutlet weak var name: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
override func layoutSubviews() {
super.layoutSubviews()
contentView.layer.cornerRadius = 5
contentView.layer.borderColor = UIColor.white.cgColor
contentView.layer.borderWidth = 5
}
}
Topic:
Programming Languages
SubTopic:
Swift
Tags: