Handle cell reuse in showing delete buttons in red

Signed-off-by: Roopesh Chander <roop@roopc.net>
This commit is contained in:
Roopesh Chander 2018-11-02 13:24:10 +05:30
parent e7a1f142fb
commit 90af773fef
2 changed files with 16 additions and 2 deletions

View File

@ -198,7 +198,7 @@ extension TunnelDetailTableViewController {
// Delete configuration // Delete configuration
let cell = tableView.dequeueReusableCell(withIdentifier: TunnelDetailTableViewButtonCell.id, for: indexPath) as! TunnelDetailTableViewButtonCell let cell = tableView.dequeueReusableCell(withIdentifier: TunnelDetailTableViewButtonCell.id, for: indexPath) as! TunnelDetailTableViewButtonCell
cell.buttonText = "Delete tunnel" cell.buttonText = "Delete tunnel"
cell.button.tintColor = UIColor.red cell.hasDestructiveAction = true
cell.onTapped = { [weak self] in cell.onTapped = { [weak self] in
guard let s = self else { return } guard let s = self else { return }
s.showConfirmationAlert(message: "Delete this tunnel?", buttonTitle: "Delete", from: cell) { [weak s] in s.showConfirmationAlert(message: "Delete this tunnel?", buttonTitle: "Delete", from: cell) { [weak s] in
@ -336,12 +336,18 @@ class TunnelDetailTableViewButtonCell: UITableViewCell {
get { return button.title(for: .normal) ?? "" } get { return button.title(for: .normal) ?? "" }
set(value) { button.setTitle(value, for: .normal) } set(value) { button.setTitle(value, for: .normal) }
} }
var hasDestructiveAction: Bool {
get { return button.tintColor == UIColor.red }
set(value) { button.tintColor = value ? UIColor.red : buttonStandardTintColor }
}
var onTapped: (() -> Void)? = nil var onTapped: (() -> Void)? = nil
let button: UIButton let button: UIButton
var buttonStandardTintColor: UIColor
override init(style: UITableViewCellStyle, reuseIdentifier: String?) { override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
button = UIButton(type: .system) button = UIButton(type: .system)
buttonStandardTintColor = button.tintColor
super.init(style: style, reuseIdentifier: reuseIdentifier) super.init(style: style, reuseIdentifier: reuseIdentifier)
contentView.addSubview(button) contentView.addSubview(button)
button.translatesAutoresizingMaskIntoConstraints = false button.translatesAutoresizingMaskIntoConstraints = false
@ -364,5 +370,6 @@ class TunnelDetailTableViewButtonCell: UITableViewCell {
super.prepareForReuse() super.prepareForReuse()
buttonText = "" buttonText = ""
onTapped = nil onTapped = nil
hasDestructiveAction = false
} }
} }

View File

@ -240,7 +240,7 @@ extension TunnelEditTableViewController {
if (field == .deletePeer) { if (field == .deletePeer) {
let cell = tableView.dequeueReusableCell(withIdentifier: TunnelEditTableViewButtonCell.id, for: indexPath) as! TunnelEditTableViewButtonCell let cell = tableView.dequeueReusableCell(withIdentifier: TunnelEditTableViewButtonCell.id, for: indexPath) as! TunnelEditTableViewButtonCell
cell.buttonText = field.rawValue cell.buttonText = field.rawValue
cell.button.tintColor = UIColor.red cell.hasDestructiveAction = true
cell.onTapped = { [weak self, weak peerData] in cell.onTapped = { [weak self, weak peerData] in
guard let peerData = peerData else { return } guard let peerData = peerData else { return }
guard let s = self else { return } guard let s = self else { return }
@ -512,12 +512,18 @@ class TunnelEditTableViewButtonCell: UITableViewCell {
get { return button.title(for: .normal) ?? "" } get { return button.title(for: .normal) ?? "" }
set(value) { button.setTitle(value, for: .normal) } set(value) { button.setTitle(value, for: .normal) }
} }
var hasDestructiveAction: Bool {
get { return button.tintColor == UIColor.red }
set(value) { button.tintColor = value ? UIColor.red : buttonStandardTintColor }
}
var onTapped: (() -> Void)? = nil var onTapped: (() -> Void)? = nil
let button: UIButton let button: UIButton
var buttonStandardTintColor: UIColor
override init(style: UITableViewCellStyle, reuseIdentifier: String?) { override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
button = UIButton(type: .system) button = UIButton(type: .system)
buttonStandardTintColor = button.tintColor
super.init(style: style, reuseIdentifier: reuseIdentifier) super.init(style: style, reuseIdentifier: reuseIdentifier)
contentView.addSubview(button) contentView.addSubview(button)
button.translatesAutoresizingMaskIntoConstraints = false button.translatesAutoresizingMaskIntoConstraints = false
@ -540,6 +546,7 @@ class TunnelEditTableViewButtonCell: UITableViewCell {
super.prepareForReuse() super.prepareForReuse()
buttonText = "" buttonText = ""
onTapped = nil onTapped = nil
hasDestructiveAction = false
} }
} }