JCButtonCell.swift 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. //
  2. // JCButtonCell.swift
  3. // JChat
  4. //
  5. // Created by deng on 2017/3/28.
  6. // Copyright © 2017年 HXHG. All rights reserved.
  7. //
  8. import UIKit
  9. @objc public protocol JCButtonCellDelegate: NSObjectProtocol {
  10. @objc optional func buttonCell(clickButton button: UIButton)
  11. }
  12. class JCButtonCell: UITableViewCell {
  13. open weak var delegate: JCButtonCellDelegate?
  14. var buttonTitle: String {
  15. get {
  16. return (button.titleLabel?.text)!
  17. }
  18. set {
  19. button.setTitle(newValue, for: .normal)
  20. }
  21. }
  22. var buttonColor: UIColor {
  23. get {
  24. return color
  25. }
  26. set {
  27. color = newValue
  28. button.setBackgroundImage(UIImage.createImage(color: color, size: CGSize(width: self.contentView.width - 30, height: self.contentView.height)), for: .normal)
  29. }
  30. }
  31. override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
  32. super.init(style: style, reuseIdentifier: reuseIdentifier)
  33. _init()
  34. }
  35. required init?(coder aDecoder: NSCoder) {
  36. super.init(coder: aDecoder)
  37. _init()
  38. }
  39. override func awakeFromNib() {
  40. super.awakeFromNib()
  41. _init()
  42. }
  43. private var color = UIColor(netHex: 0xFB4747)
  44. private lazy var button: UIButton = {
  45. let button = UIButton()
  46. button.addTarget(self, action: #selector(_click(_:)), for: .touchUpInside)
  47. button.titleLabel?.font = UIFont.systemFont(ofSize: 16)
  48. button.setTitle("退出登录", for: .normal)
  49. button.layer.cornerRadius = 3.0
  50. button.layer.masksToBounds = true
  51. return button
  52. }()
  53. //MARK: - private func
  54. private func _init() {
  55. backgroundColor = .clear
  56. button.setBackgroundImage(UIImage.createImage(color: color, size: CGSize(width: contentView.width - 30, height: contentView.height)), for: .normal)
  57. contentView.addSubview(button)
  58. addConstraint(_JCLayoutConstraintMake(button, .left, .equal, contentView, .left, 15))
  59. addConstraint(_JCLayoutConstraintMake(button, .right, .equal, contentView, .right, -15))
  60. addConstraint(_JCLayoutConstraintMake(button, .top, .equal, contentView, .top))
  61. addConstraint(_JCLayoutConstraintMake(button, .bottom, .equal, contentView, .bottom))
  62. }
  63. //MARK: - click func
  64. func _click(_ sender: UIButton) {
  65. delegate?.buttonCell?(clickButton: sender)
  66. }
  67. }