JCDoubleButtonCell.swift 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. //
  2. // JCDoubleButtonCell.swift
  3. // JChat
  4. //
  5. // Created by deng on 2017/5/16.
  6. // Copyright © 2017年 HXHG. All rights reserved.
  7. //
  8. import UIKit
  9. @objc public protocol JCDoubleButtonCellDelegate: NSObjectProtocol {
  10. @objc optional func doubleButtonCell(clickLeftButton button: UIButton)
  11. @objc optional func doubleButtonCell(clickRightButton button: UIButton)
  12. }
  13. class JCDoubleButtonCell: UITableViewCell {
  14. open weak var delegate: JCDoubleButtonCellDelegate?
  15. var leftButtonTitle: String {
  16. get {
  17. return leftButton.titleLabel?.text ?? ""
  18. }
  19. set {
  20. leftButton.setTitle(newValue, for: .normal)
  21. }
  22. }
  23. var leftButtonColor: UIColor {
  24. get {
  25. return color
  26. }
  27. set {
  28. color = newValue
  29. leftButton.backgroundColor = newValue
  30. }
  31. }
  32. var rightButtonTitle: String {
  33. get {
  34. return rightButton.titleLabel?.text ?? ""
  35. }
  36. set {
  37. rightButton.setTitle(newValue, for: .normal)
  38. }
  39. }
  40. var rightButtonColor: UIColor {
  41. get {
  42. return color
  43. }
  44. set {
  45. color = newValue
  46. rightButton.backgroundColor = newValue
  47. }
  48. }
  49. required init?(coder aDecoder: NSCoder) {
  50. super.init(coder: aDecoder)
  51. _init()
  52. }
  53. override func awakeFromNib() {
  54. super.awakeFromNib()
  55. _init()
  56. }
  57. private var color = UIColor(netHex: 0xfb4747)
  58. private lazy var leftButton: UIButton = UIButton()
  59. private lazy var rightButton: UIButton = UIButton()
  60. //MARK: - private func
  61. private func _init() {
  62. backgroundColor = .clear
  63. leftButton.addTarget(self, action: #selector(_clickLeftButton(_:)), for: .touchUpInside)
  64. leftButton.setTitle("加好友", for: .normal)
  65. leftButton.setTitleColor(.black, for: .normal)
  66. leftButton.backgroundColor = .white
  67. leftButton.layer.cornerRadius = 3.0
  68. leftButton.layer.masksToBounds = true
  69. contentView.addSubview(leftButton)
  70. rightButton.addTarget(self, action: #selector(_clickRightButton(_:)), for: .touchUpInside)
  71. rightButton.setTitle("发送消息", for: .normal)
  72. rightButton.layer.cornerRadius = 3.0
  73. rightButton.layer.masksToBounds = true
  74. rightButton.backgroundColor = color
  75. contentView.addSubview(rightButton)
  76. addConstraint(_JCLayoutConstraintMake(leftButton, .left, .equal, contentView, .left, 15))
  77. addConstraint(_JCLayoutConstraintMake(leftButton, .right, .equal, contentView, .centerX, -12.5))
  78. addConstraint(_JCLayoutConstraintMake(leftButton, .top, .equal, contentView, .top))
  79. addConstraint(_JCLayoutConstraintMake(leftButton, .bottom, .equal, contentView, .bottom))
  80. addConstraint(_JCLayoutConstraintMake(rightButton, .left, .equal, contentView, .centerX, 12.5))
  81. addConstraint(_JCLayoutConstraintMake(rightButton, .right, .equal, contentView, .right, -15))
  82. addConstraint(_JCLayoutConstraintMake(rightButton, .top, .equal, contentView, .top))
  83. addConstraint(_JCLayoutConstraintMake(rightButton, .bottom, .equal, contentView, .bottom))
  84. }
  85. //MARK: - click func
  86. func _clickLeftButton(_ sender: UIButton) {
  87. delegate?.doubleButtonCell?(clickLeftButton: sender)
  88. }
  89. func _clickRightButton(_ sender: UIButton) {
  90. delegate?.doubleButtonCell?(clickRightButton: sender)
  91. }
  92. }