|
2 | 2 | // SFAlertCell.swift
|
3 | 3 | // SFAlertController
|
4 | 4 | //
|
5 |
| -// Created by 蔡龙君 on 2019/8/28. |
| 5 | +// Created by 花菜 on 2019/8/28. |
6 | 6 | // Copyright © 2019 花菜. All rights reserved.
|
7 | 7 | //
|
8 | 8 |
|
9 | 9 | import UIKit
|
10 | 10 |
|
11 | 11 | class SFAlertCell: UITableViewCell {
|
| 12 | + static var identifier = "SFAlertCell" |
| 13 | + /// 分割线 |
| 14 | + private(set) lazy var separatorView: UIView = { |
| 15 | + $0.translatesAutoresizingMaskIntoConstraints = false |
| 16 | + $0.backgroundColor = UIColor(red: 0.783922, green: 0.780392, blue: 0.8, alpha: 1) |
| 17 | + return $0 |
| 18 | + }(UIView()) |
| 19 | + |
| 20 | + private lazy var leftButton: UIButton = { |
| 21 | + $0.setTitleColor(UIColor(red: 0, green: 0.48, blue: 1, alpha: 1), for: .selected) |
| 22 | + $0.setTitleColor(UIColor.black, for: .normal) |
| 23 | + $0.backgroundColor = UIColor.white |
| 24 | + $0.setTitleColor(UIColor.black.withAlphaComponent(0.25), for: .disabled) |
| 25 | + $0.titleLabel?.font = UIFont.systemFont(ofSize: 17, weight: .semibold) |
| 26 | + $0.addTarget(self, action: #selector(leftButtonTapped), for: .touchUpInside) |
| 27 | + return $0 |
| 28 | + }(UIButton(type: .custom)) |
| 29 | + |
| 30 | + private lazy var rightButton: UIButton = { |
| 31 | + $0.isHidden = true |
| 32 | + $0.setTitleColor(UIColor(red: 0, green: 0.48, blue: 1, alpha: 1), for: .selected) |
| 33 | + $0.setTitleColor(UIColor.black, for: .normal) |
| 34 | + $0.backgroundColor = UIColor.white |
| 35 | + $0.setTitleColor(UIColor.black.withAlphaComponent(0.25), for: .disabled) |
| 36 | + $0.addTarget(self, action: #selector(rightButtonTapped), for: .touchUpInside) |
| 37 | + return $0 |
| 38 | + }(UIButton(type: .custom)) |
| 39 | + |
| 40 | + var leftCompletion: (() -> Void)? |
| 41 | + var rightCompletion: (() -> Void)? |
| 42 | + |
| 43 | + /// 内容视图 |
| 44 | + private lazy var stackView: UIStackView = { |
| 45 | + $0.translatesAutoresizingMaskIntoConstraints = false |
| 46 | + $0.alignment = .fill |
| 47 | + $0.distribution = .fillProportionally |
| 48 | + $0.axis = .horizontal |
| 49 | + return $0 |
| 50 | + }(UIStackView(arrangedSubviews: [leftButton, rightButton])) |
| 51 | + |
| 52 | + override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) { |
| 53 | + super.init(style: style, reuseIdentifier: reuseIdentifier) |
| 54 | + configSubviews() |
| 55 | + } |
| 56 | + |
| 57 | + required init?(coder aDecoder: NSCoder) { |
| 58 | + super.init(coder: aDecoder) |
| 59 | + configSubviews() |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +/// public |
| 64 | +extension SFAlertCell { |
| 65 | + func config(using leftButtonType: SFAlertController.AlertButton, rightButtonType: SFAlertController.AlertButton? = nil) { |
| 66 | + |
| 67 | + configButton(leftButton, using: leftButtonType) |
| 68 | + guard let rightButtonType = rightButtonType else { |
| 69 | + separatorView.isHidden = true |
| 70 | + rightButton.isHidden = true |
| 71 | + return |
| 72 | + } |
| 73 | + separatorView.isHidden = false |
| 74 | + configButton(rightButton, using: rightButtonType) |
| 75 | + } |
| 76 | +} |
12 | 77 |
|
13 |
| - override func awakeFromNib() { |
14 |
| - super.awakeFromNib() |
15 |
| - // Initialization code |
| 78 | +/// private |
| 79 | +extension SFAlertCell { |
| 80 | + |
| 81 | + private func configSubviews() { |
| 82 | + contentView.addSubview(stackView) |
| 83 | + contentView.addSubview(separatorView) |
| 84 | + selectionStyle = .none |
| 85 | + contentView.backgroundColor = UIColor(red: 0.783922, green: 0.780392, blue: 0.8, alpha: 1) |
| 86 | + stackView.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 0.5).isActive = true |
| 87 | + stackView.leftAnchor.constraint(equalTo: contentView.leftAnchor).isActive = true |
| 88 | + stackView.rightAnchor.constraint(equalTo: contentView.rightAnchor).isActive = true |
| 89 | + stackView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor).isActive = true |
| 90 | + |
| 91 | + separatorView.topAnchor.constraint(equalTo: contentView.topAnchor).isActive = true |
| 92 | + separatorView.centerXAnchor.constraint(equalTo: contentView.centerXAnchor).isActive = true |
| 93 | + separatorView.widthAnchor.constraint(equalToConstant: 0.5).isActive = true |
| 94 | + separatorView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor).isActive = true |
16 | 95 | }
|
17 | 96 |
|
18 |
| - override func setSelected(_ selected: Bool, animated: Bool) { |
19 |
| - super.setSelected(selected, animated: animated) |
| 97 | + private func configButton(_ button: UIButton, using type: SFAlertController.AlertButton) { |
| 98 | + switch type { |
| 99 | + case .cancel: |
| 100 | + button.isEnabled = true |
| 101 | + button.isSelected = false |
| 102 | + case .disabled: |
| 103 | + button.isEnabled = false |
| 104 | + button.isSelected = false |
| 105 | + case .default: |
| 106 | + button.isEnabled = true |
| 107 | + button.isSelected = false |
| 108 | + case .destructive: |
| 109 | + button.isEnabled = true |
| 110 | + button.isSelected = true |
| 111 | + } |
| 112 | + button.setTitle(type.title, for: .normal) |
| 113 | + button.isHidden = false |
| 114 | + } |
20 | 115 |
|
21 |
| - // Configure the view for the selected state |
| 116 | + @objc private func leftButtonTapped() { |
| 117 | + leftCompletion?() |
22 | 118 | }
|
23 | 119 |
|
| 120 | + @objc private func rightButtonTapped() { |
| 121 | + rightCompletion?() |
| 122 | + } |
24 | 123 | }
|
0 commit comments