Advertisement
Guest User

UITableView Stacked Problem

a guest
May 9th, 2022
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 5.23 KB | None | 0 0
  1. import UIKit
  2.  
  3. class UserViewController: UITableViewController {
  4.    
  5.     var API = DummyAPI(appId: "625534f6d7e95833f9570907")
  6.     var id = ""
  7.    
  8.     var profileUsernameView = UILabel()
  9.     var profilePictureView = UIImageView()
  10.     var genderView = UILabel()
  11.     var emailView = UILabel()
  12.     var dateOfBirthView = UILabel()
  13.     var phoneView = UILabel()
  14.     var locationView = UILabel()
  15.     var registeredDateView = UILabel()
  16.     var updatedDateView = UILabel()
  17.    
  18.    
  19.     override func viewWillAppear(_ animated: Bool) {
  20.         super.viewWillAppear(animated)
  21.         loadUser()
  22.  
  23.  
  24.     }
  25.    
  26.  
  27.     override func viewDidLoad() {
  28.         super.viewDidLoad()
  29.         tableView.separatorStyle = .none
  30.         tableView.register(UITableViewCell.self, forCellReuseIdentifier: "UITableViewCell")
  31.     }
  32.    
  33.     override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  34.         let cell = tableView.dequeueReusableCell(withIdentifier: "UITableViewCell", for: indexPath)
  35.         cell.selectionStyle = .none
  36.         cell.backgroundColor = .systemBackground
  37.        
  38.         cell.contentView.addSubview(profileUsernameView)
  39.         cell.contentView.addSubview(profilePictureView)
  40.         cell.contentView.addSubview(emailView)
  41.         cell.contentView.addSubview(phoneView)
  42.         cell.contentView.addSubview(locationView)
  43.         cell.contentView.addSubview(registeredDateView)
  44.  
  45.         profileUsernameView.translatesAutoresizingMaskIntoConstraints = false
  46.         profileUsernameView.font = UIFont.systemFont(ofSize: 18, weight: .semibold)
  47.         profileUsernameView.numberOfLines = 0
  48.         profileUsernameView.textAlignment = .left
  49.        
  50.         profilePictureView.translatesAutoresizingMaskIntoConstraints = false
  51.         profilePictureView.clipsToBounds = true
  52.         profilePictureView.layer.cornerRadius = 45
  53.        
  54.         emailView.translatesAutoresizingMaskIntoConstraints = false
  55.        
  56.         phoneView.translatesAutoresizingMaskIntoConstraints = false
  57.        
  58.         locationView.translatesAutoresizingMaskIntoConstraints = false
  59.         locationView.numberOfLines = 0
  60.         locationView.textAlignment = .left
  61.        
  62.         registeredDateView.translatesAutoresizingMaskIntoConstraints = false
  63.  
  64.         NSLayoutConstraint.activate([
  65.  
  66.             profilePictureView.widthAnchor.constraint(equalToConstant: 90),
  67.             profilePictureView.heightAnchor.constraint(equalToConstant: 90),
  68.             profilePictureView.topAnchor.constraint(equalTo: cell.contentView.topAnchor,constant: 5),
  69.             profilePictureView.leadingAnchor.constraint(equalTo: cell.layoutMarginsGuide.leadingAnchor),
  70.            
  71.             profileUsernameView.topAnchor.constraint(equalTo: profilePictureView.bottomAnchor, constant: 10),
  72.             profileUsernameView.leadingAnchor.constraint(equalTo: cell.contentView.layoutMarginsGuide.leadingAnchor),
  73.            
  74.             locationView.topAnchor.constraint(equalTo: profileUsernameView.bottomAnchor, constant: 2),
  75.             locationView.leadingAnchor.constraint(equalTo: cell.contentView.layoutMarginsGuide.leadingAnchor),
  76.            
  77.             phoneView.topAnchor.constraint(equalTo: locationView.bottomAnchor, constant: 2),
  78.             phoneView.leadingAnchor.constraint(equalTo: cell.contentView.layoutMarginsGuide.leadingAnchor),
  79.            
  80.             emailView.topAnchor.constraint(equalTo: phoneView.bottomAnchor, constant: 2),
  81.             emailView.leadingAnchor.constraint(equalTo: cell.contentView.layoutMarginsGuide.leadingAnchor),
  82.  
  83.             registeredDateView.topAnchor.constraint(equalTo: emailView.bottomAnchor, constant: 2),
  84.             registeredDateView.leadingAnchor.constraint(equalTo: cell.contentView.layoutMarginsGuide.leadingAnchor),
  85.            
  86.             cell.contentView.heightAnchor.constraint(greaterThanOrEqualToConstant: 350)
  87.  
  88.         ])
  89.        
  90.         self.tableView.beginUpdates()
  91.         self.tableView.endUpdates()
  92.        
  93.         return cell
  94.     }
  95.    
  96.     override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  97.         return 1
  98.     }
  99.  
  100.  
  101. }
  102.  
  103. extension UserViewController {
  104.    
  105.     func setUser(with data: UserModel) {
  106.        
  107.         self.title = data.firstName.lowercased() + data.lastName.lowercased()
  108.         self.profilePictureView.load(url: URL(string: data.picture)!)
  109.         self.profileUsernameView.text = data.firstName + " " + data.lastName
  110.         self.genderView.text = data.gender
  111.         self.emailView.text = data.email
  112.         self.dateOfBirthView.text = data.dateOfBirth
  113.         self.phoneView.text = data.phone
  114.         self.locationView.text = "📍 " + data.location.city + ", " + data.location.country
  115.  
  116.  
  117.         self.registeredDateView.text = "Joined since \((data.registerDate).prefix(10))"
  118.         self.updatedDateView.text = "Last updated \(data.updatedDate)"
  119.  
  120.     }
  121.    
  122.     func loadUser() {
  123.     API.getDetailUser(id: id) { [weak self] (result) in
  124.         guard let _self = self else { return }
  125.         switch result {
  126.         case let .success(data):
  127.             _self.setUser(with: data)
  128.         case .failure:
  129.             break
  130.            
  131.         }
  132.        
  133.     }
  134.        
  135.     }
  136.        
  137. }
  138.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement