Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import UIKit
- class UserViewController: UITableViewController {
- var API = DummyAPI(appId: "625534f6d7e95833f9570907")
- var id = ""
- var profileUsernameView = UILabel()
- var profilePictureView = UIImageView()
- var genderView = UILabel()
- var emailView = UILabel()
- var dateOfBirthView = UILabel()
- var phoneView = UILabel()
- var locationView = UILabel()
- var registeredDateView = UILabel()
- var updatedDateView = UILabel()
- override func viewWillAppear(_ animated: Bool) {
- super.viewWillAppear(animated)
- loadUser()
- }
- override func viewDidLoad() {
- super.viewDidLoad()
- tableView.separatorStyle = .none
- tableView.register(UITableViewCell.self, forCellReuseIdentifier: "UITableViewCell")
- }
- override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
- let cell = tableView.dequeueReusableCell(withIdentifier: "UITableViewCell", for: indexPath)
- cell.selectionStyle = .none
- cell.backgroundColor = .systemBackground
- cell.contentView.addSubview(profileUsernameView)
- cell.contentView.addSubview(profilePictureView)
- cell.contentView.addSubview(emailView)
- cell.contentView.addSubview(phoneView)
- cell.contentView.addSubview(locationView)
- cell.contentView.addSubview(registeredDateView)
- profileUsernameView.translatesAutoresizingMaskIntoConstraints = false
- profileUsernameView.font = UIFont.systemFont(ofSize: 18, weight: .semibold)
- profileUsernameView.numberOfLines = 0
- profileUsernameView.textAlignment = .left
- profilePictureView.translatesAutoresizingMaskIntoConstraints = false
- profilePictureView.clipsToBounds = true
- profilePictureView.layer.cornerRadius = 45
- emailView.translatesAutoresizingMaskIntoConstraints = false
- phoneView.translatesAutoresizingMaskIntoConstraints = false
- locationView.translatesAutoresizingMaskIntoConstraints = false
- locationView.numberOfLines = 0
- locationView.textAlignment = .left
- registeredDateView.translatesAutoresizingMaskIntoConstraints = false
- NSLayoutConstraint.activate([
- profilePictureView.widthAnchor.constraint(equalToConstant: 90),
- profilePictureView.heightAnchor.constraint(equalToConstant: 90),
- profilePictureView.topAnchor.constraint(equalTo: cell.contentView.topAnchor,constant: 5),
- profilePictureView.leadingAnchor.constraint(equalTo: cell.layoutMarginsGuide.leadingAnchor),
- profileUsernameView.topAnchor.constraint(equalTo: profilePictureView.bottomAnchor, constant: 10),
- profileUsernameView.leadingAnchor.constraint(equalTo: cell.contentView.layoutMarginsGuide.leadingAnchor),
- locationView.topAnchor.constraint(equalTo: profileUsernameView.bottomAnchor, constant: 2),
- locationView.leadingAnchor.constraint(equalTo: cell.contentView.layoutMarginsGuide.leadingAnchor),
- phoneView.topAnchor.constraint(equalTo: locationView.bottomAnchor, constant: 2),
- phoneView.leadingAnchor.constraint(equalTo: cell.contentView.layoutMarginsGuide.leadingAnchor),
- emailView.topAnchor.constraint(equalTo: phoneView.bottomAnchor, constant: 2),
- emailView.leadingAnchor.constraint(equalTo: cell.contentView.layoutMarginsGuide.leadingAnchor),
- registeredDateView.topAnchor.constraint(equalTo: emailView.bottomAnchor, constant: 2),
- registeredDateView.leadingAnchor.constraint(equalTo: cell.contentView.layoutMarginsGuide.leadingAnchor),
- cell.contentView.heightAnchor.constraint(greaterThanOrEqualToConstant: 350)
- ])
- self.tableView.beginUpdates()
- self.tableView.endUpdates()
- return cell
- }
- override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
- return 1
- }
- }
- extension UserViewController {
- func setUser(with data: UserModel) {
- self.title = data.firstName.lowercased() + data.lastName.lowercased()
- self.profilePictureView.load(url: URL(string: data.picture)!)
- self.profileUsernameView.text = data.firstName + " " + data.lastName
- self.genderView.text = data.gender
- self.emailView.text = data.email
- self.dateOfBirthView.text = data.dateOfBirth
- self.phoneView.text = data.phone
- self.locationView.text = "📍 " + data.location.city + ", " + data.location.country
- self.registeredDateView.text = "Joined since \((data.registerDate).prefix(10))"
- self.updatedDateView.text = "Last updated \(data.updatedDate)"
- }
- func loadUser() {
- API.getDetailUser(id: id) { [weak self] (result) in
- guard let _self = self else { return }
- switch result {
- case let .success(data):
- _self.setUser(with: data)
- case .failure:
- break
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement