|
8 | 8 | import UIKit |
9 | 9 |
|
10 | 10 | class UserProfileView: UIView { |
| 11 | + |
| 12 | + var userData = ["Phone", "E-mail", "Address"] |
| 13 | + var userModel = ["+55(11)99999-9999", "user@devpass.com", "Rua Bela Cintra, 495"] |
| 14 | + var testModel = ["Personal data", "Bank account", "Taxes"] |
| 15 | + var generalModel = ["Need help?", "About Devpass"] |
| 16 | + var appModel = ["App Version"] |
| 17 | + var versionModel = ["1.0 (1)"] |
| 18 | + |
| 19 | + private lazy var headerStackView: UserProfileHeaderView = { |
| 20 | + let stack = UserProfileHeaderView() |
| 21 | + stack.translatesAutoresizingMaskIntoConstraints = false |
| 22 | + return stack |
| 23 | + }() |
| 24 | + |
| 25 | + private lazy var tableView: UITableView = { |
| 26 | + let tableView = UITableView(frame: .zero, style: .insetGrouped) |
| 27 | + tableView.translatesAutoresizingMaskIntoConstraints = false |
| 28 | + tableView.register(UserProfileViewCell.self, forCellReuseIdentifier: UserProfileViewCell.identifier) |
| 29 | +// tableView.register(UserProfileViewGeneralCell.self, forCellReuseIdentifier: UserProfileViewGeneralCell.identifier) |
| 30 | + tableView.register(UITableViewCell.self, forCellReuseIdentifier: UITableViewCell.reuseIdentifier) |
| 31 | + return tableView |
| 32 | + }() |
| 33 | + |
| 34 | + override init(frame: CGRect) { |
| 35 | + super.init(frame: frame) |
| 36 | + self.tableView.delegate = self |
| 37 | + self.tableView.dataSource = self |
| 38 | + self.setupView() |
| 39 | + } |
| 40 | + |
| 41 | + required init?(coder: NSCoder) { |
| 42 | + fatalError("init(coder:) has not been implemented") |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +extension UserProfileView: ViewCodable { |
| 47 | + |
| 48 | + func buildHierarchy() { |
| 49 | + addSubview(headerStackView) |
| 50 | + addSubview(tableView) |
| 51 | + } |
| 52 | + |
| 53 | + func setupConstraints() { |
| 54 | + NSLayoutConstraint.activate([ |
| 55 | + headerStackView.topAnchor.constraint(equalTo: topAnchor, constant: 32), |
| 56 | + headerStackView.leadingAnchor.constraint(equalTo: leadingAnchor), |
| 57 | + headerStackView.trailingAnchor.constraint(equalTo: trailingAnchor), |
| 58 | + |
| 59 | + tableView.topAnchor.constraint(lessThanOrEqualTo: headerStackView.bottomAnchor, constant: 8), |
| 60 | + tableView.leadingAnchor.constraint(equalTo: leadingAnchor), |
| 61 | + tableView.trailingAnchor.constraint(equalTo: trailingAnchor), |
| 62 | + tableView.bottomAnchor.constraint(equalTo: bottomAnchor) |
| 63 | + ]) |
| 64 | + } |
| 65 | + |
| 66 | + func applyAdditionalChanges() { |
| 67 | + backgroundColor = .init(hexString: "#F2F2F7") |
| 68 | + } |
| 69 | +} |
11 | 70 |
|
| 71 | +extension UserProfileView: UITableViewDelegate, UITableViewDataSource { |
| 72 | + |
| 73 | + func numberOfSections(in tableView: UITableView) -> Int { |
| 74 | + return 2 |
| 75 | + } |
| 76 | + |
| 77 | + func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { |
| 78 | + if section == 0 { |
| 79 | + return "MY ACCOUNT" |
| 80 | + } else { |
| 81 | + return "GENERAL" |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { |
| 86 | + if section == 0 { |
| 87 | + return userData.count + testModel.count |
| 88 | + } else { |
| 89 | + return generalModel.count + appModel.count |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { |
| 94 | + if indexPath.section == 0 && indexPath.row < 3 { |
| 95 | + let cell = tableView.dequeueReusableCell(withIdentifier: UserProfileViewCell.identifier, for: indexPath) as! UserProfileViewCell |
| 96 | + cell.configure(leftLabel: userData[indexPath.row] , rightLabel: userModel[indexPath.row]) |
| 97 | + return cell |
| 98 | + } else if indexPath.section == 0 && indexPath.row > 2 && indexPath.row < 6 { |
| 99 | + let buttonCell: UITableViewCell = .createCell(for: tableView, at: indexPath)! |
| 100 | + buttonCell.textLabel?.text = self.testModel[indexPath.row - 3] |
| 101 | + buttonCell.accessoryType = .disclosureIndicator |
| 102 | + return buttonCell |
| 103 | + } else if indexPath.section == 1 && indexPath.row < 2 { |
| 104 | + let buttonCell: UITableViewCell = .createCell(for: tableView, at: indexPath)! |
| 105 | + buttonCell.textLabel?.text = generalModel[indexPath.row] |
| 106 | + buttonCell.accessoryType = .disclosureIndicator |
| 107 | + return buttonCell |
| 108 | + } else { |
| 109 | + let versionCell = tableView.dequeueReusableCell(withIdentifier: UserProfileViewCell.identifier, for: indexPath) as! UserProfileViewCell |
| 110 | + versionCell.configure(leftLabel: appModel[indexPath.row - 2], rightLabel: versionModel[indexPath.row - 2]) |
| 111 | + return versionCell |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { |
| 116 | + // Chamar um método |
| 117 | + |
| 118 | + tableView.deselectRow(at: indexPath, animated: false) |
| 119 | + } |
12 | 120 | } |
0 commit comments