Guest User

Untitled

a guest
Dec 9th, 2018
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.94 KB | None | 0 0
  1. import UIKit
  2.  
  3. struct Contact {
  4. let name: String
  5. let email: String
  6. }
  7.  
  8. class ContactTileAvatar: UIView {
  9. @IBOutlet var label: UILabel!
  10.  
  11. override func layoutSubviews() {
  12. super.layoutSubviews()
  13. layer.cornerRadius = frame.size.width / 2.0
  14. }
  15.  
  16. var name: String? {
  17. didSet {
  18. if let name = name, name.count > 0 {
  19. label.text = String(name.prefix(1))
  20. }
  21. }
  22. }
  23. }
  24.  
  25. class ContactCell: UITableViewCell {
  26. @IBOutlet var leading: ContactTileAvatar!
  27. @IBOutlet var title: UILabel!
  28. @IBOutlet var subtitle: UILabel!
  29. var contact: Contact? {
  30. didSet {
  31. title.text = contact?.name
  32. subtitle.text = contact?.email
  33. leading.name = contact?.name
  34. }
  35. }
  36. }
  37.  
  38. class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
  39.  
  40. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  41. return allContacts.count
  42. }
  43.  
  44. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  45. guard let cell = tableView.dequeueReusableCell(withIdentifier: "ContactCell") as? ContactCell else {
  46. fatalError("cell not registered")
  47. }
  48. cell.contact = allContacts[indexPath.row]
  49. return cell
  50. }
  51.  
  52. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  53. return 73.0
  54. }
  55. }
  56.  
  57. let allContacts = [
  58. Contact(name: "Isa Tusa", email: "isa.tusa@me.com"),
  59. Contact(name: "Racquel Ricciardi", email: "racquel.ricciardi@me.com"),
  60. Contact(name: "Teresita Mccubbin", email: "teresita.mccubbin@me.com"),
  61. Contact(name: "Rhoda Hassinger", email: "rhoda.hassinger@me.com"),
  62. Contact(name: "Carson Cupps", email: "carson.cupps@me.com"),
  63. Contact(name: "Devora Nantz", email: "devora.nantz@me.com"),
  64. Contact(name: "Tyisha Primus", email: "tyisha.primus@me.com"),
  65. Contact(name: "Muriel Lewellyn", email: "muriel.lewellyn@me.com"),
  66. Contact(name: "Hunter Giraud", email: "hunter.giraud@me.com"),
  67. ]
Add Comment
Please, Sign In to add comment