QSLContactCell.swift 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. //
  2. // QSLContactCell.swift
  3. // QuickSearchLocation
  4. //
  5. // Created by Destiny on 2024/12/11.
  6. //
  7. import UIKit
  8. protocol QSLContactCellDelegate: NSObjectProtocol {
  9. func resortClickAction(model: QSLContactModel)
  10. func favorClickAction(model: QSLContactModel)
  11. func deleteClickAction(model: QSLContactModel)
  12. }
  13. class QSLContactCell: UITableViewCell {
  14. weak var delegate: QSLContactCellDelegate?
  15. var model: QSLContactModel?
  16. lazy var bgView: UIView = {
  17. let view = UIView()
  18. view.addRadius(radius: 8.rpx)
  19. view.backgroundColor = .white
  20. return view
  21. }()
  22. lazy var avatarImageView: UIImageView = {
  23. let imageView = UIImageView()
  24. imageView.image = UIImage(named: "friends_cell_other_avatar")
  25. return imageView
  26. }()
  27. lazy var nameLabel: UILabel = {
  28. let label = UILabel()
  29. label.text("儿子")
  30. label.mediumFont(16)
  31. label.textColor = QSLColor.Color_202020
  32. return label
  33. }()
  34. lazy var resortBtn: UIButton = {
  35. let btn = UIButton()
  36. btn.image(UIImage(named: "contact_resort_btn"))
  37. btn.addTarget(self, action: #selector(resortBtnAction), for: .touchUpInside)
  38. return btn
  39. }()
  40. lazy var lineView: UIView = {
  41. let view = UIView()
  42. view.backgroundColor = .hexStringColor(hexString: "#F0F0F0", alpha: 0.2)
  43. return view
  44. }()
  45. lazy var selectBtn: UIButton = {
  46. let btn = UIButton()
  47. btn.image(UIImage(named: "contact_btn_unselected"), .normal)
  48. btn.image(UIImage(named: "contact_btn_selected"), .selected)
  49. btn.title("默认该联系人")
  50. btn.font(13)
  51. btn.textColor(QSLColor.Color_202020)
  52. btn.setImageTitleLayout(.imgLeft, spacing: 6.rpx)
  53. btn.addTarget(self, action: #selector(favorBtnAction), for: .touchUpInside)
  54. return btn
  55. }()
  56. lazy var deleteBtn: UIButton = {
  57. let btn = UIButton()
  58. btn.image(UIImage(named: "contact_delete_btn"))
  59. btn.title("删除")
  60. btn.font(13)
  61. btn.textColor(QSLColor.Color_202020)
  62. btn.setImageTitleLayout(.imgLeft, spacing: 4.rpx)
  63. btn.addTarget(self, action: #selector(deleteBtnAction), for: .touchUpInside)
  64. return btn
  65. }()
  66. override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
  67. super.init(style: style, reuseIdentifier: reuseIdentifier)
  68. initView()
  69. }
  70. required init?(coder: NSCoder) {
  71. fatalError("init(coder:) has not been implemented")
  72. }
  73. func config(model: QSLContactModel) {
  74. self.model = model
  75. self.nameLabel.text = model.remark.count > 0 ? model.remark : model.phone
  76. self.selectBtn.isSelected = model.favor
  77. }
  78. }
  79. extension QSLContactCell {
  80. @objc func resortBtnAction() {
  81. if let model = self.model {
  82. delegate?.resortClickAction(model: model)
  83. }
  84. }
  85. @objc func favorBtnAction() {
  86. if let model = self.model {
  87. delegate?.favorClickAction(model: model)
  88. }
  89. }
  90. @objc func deleteBtnAction() {
  91. if let model = self.model {
  92. delegate?.deleteClickAction(model: model)
  93. }
  94. }
  95. }
  96. extension QSLContactCell {
  97. func initView() {
  98. self.backgroundColor = .clear
  99. self.contentView.backgroundColor = .clear
  100. self.contentView.addSubview(bgView)
  101. bgView.snp.makeConstraints { make in
  102. make.edges.equalTo(0)
  103. }
  104. bgView.addSubview(avatarImageView)
  105. avatarImageView.snp.makeConstraints { make in
  106. make.size.equalTo(CGSize(width: 48.rpx, height: 48.rpx))
  107. make.left.top.equalTo(16.rpx)
  108. }
  109. bgView.addSubview(nameLabel)
  110. nameLabel.snp.makeConstraints { make in
  111. make.centerY.equalTo(avatarImageView.snp.centerY)
  112. make.left.equalTo(avatarImageView.snp.right).offset(12.rpx)
  113. }
  114. bgView.addSubview(resortBtn)
  115. resortBtn.snp.makeConstraints { make in
  116. make.size.equalTo(CGSize(width: 92.rpx, height: 32.rpx))
  117. make.right.equalTo(-16.rpx)
  118. make.centerY.equalTo(avatarImageView.snp.centerY)
  119. }
  120. bgView.addSubview(lineView)
  121. lineView.snp.makeConstraints { make in
  122. make.left.equalTo(16.rpx)
  123. make.right.equalTo(-16.rpx)
  124. make.top.equalTo(76.rpx)
  125. make.height.equalTo(1.rpx)
  126. }
  127. bgView.addSubview(selectBtn)
  128. selectBtn.snp.makeConstraints { make in
  129. make.size.equalTo(CGSize(width: 100.rpx, height: 20.rpx))
  130. make.left.equalTo(lineView.snp.left)
  131. make.top.equalTo(lineView.snp.bottom).offset(8.rpx)
  132. }
  133. bgView.addSubview(deleteBtn)
  134. deleteBtn.snp.makeConstraints { make in
  135. make.size.equalTo(CGSize(width: 50.rpx, height: 20.rpx))
  136. make.right.equalTo(lineView.snp.right)
  137. make.centerY.equalTo(selectBtn.snp.centerY)
  138. }
  139. }
  140. }