Mar*_*aka 13 uitableview uikit uilabel ios swift
我正在使用 aUITableView来显示信用卡的交易列表。如果交易是退款,我会在标签中添加删除线样式:
当重复使用该特定单元格时会出现问题。即使在重置标签的textandattributedText属性后,删除线装饰仍然存在。
下面我添加了代码的相关部分:
class TimelineViewController: UIViewController {
private lazy var tableView: UITableView = {
let tableView = UITableView.init(frame: view.frame, style: .plain)
tableView.translatesAutoresizingMaskIntoConstraints = false
tableView.register(TimelineTableViewCell.self, forCellReuseIdentifier: TimelineTableViewCell.reuseIdentifier)
tableView.dataSource = self
tableView.delegate = self
return tableView
}()
override func viewDidLoad() {
super.viewDidLoad()
addTableViewOnView()
getTimeline()
}
}
extension TimelineViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: TimelineTableViewCell.reuseIdentifier,
for: indexPath) as? TimelineTableViewCell else { fatalError() }
cell.transactionData = viewModel.timeline[indexPath.row]
return cell
}
}
Run Code Online (Sandbox Code Playgroud)
class TimelineTableViewCell: UITableViewCell {
static let reuseIdentifier = "TimelineTableViewCell"
var transactionData: TimelineResponseModel! {
didSet {
// Reset the labels
transactionDescriptionLabel.text = nil
transactionDescriptionLabel.attributedText = nil
transactionValueLabel.text = nil
transactionValueLabel.attributedText = nil
// Fill in the values
transactionDescriptionLabel.text = transactionData.description
transactionValueLabel.text = Formatter.currency.string(for: transactionData.balance)
if transactionData.isChargeback {
let value = Formatter.currency.string(for: transactionData.balance) ?? ""
transactionDescriptionLabel.attributedText = transactionData.description.strikedThrough()
transactionValueLabel.attributedText = value.strikedThrough()
}
}
}
private lazy var transactionDescriptionLabel: UILabel = {
let transactionDescriptionLabel = UILabel()
transactionDescriptionLabel.translatesAutoresizingMaskIntoConstraints = false
transactionDescriptionLabel.font = UIFont.preferredFont(forTextStyle: .footnote)
transactionDescriptionLabel.adjustsFontForContentSizeCategory = true
transactionDescriptionLabel.textColor = UIColor.activeText()
transactionDescriptionLabel.numberOfLines = 0
return transactionDescriptionLabel
}()
private lazy var transactionValueLabel: UILabel = {
let transactionValueLabel = UILabel()
transactionValueLabel.translatesAutoresizingMaskIntoConstraints = false
transactionValueLabel.font = UIFont.preferredFont(forTextStyle: .caption1).bold()
transactionValueLabel.adjustsFontForContentSizeCategory = true
transactionValueLabel.textColor = UIColor.activeText()
return transactionValueLabel
}()
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
addTransactionDescriptionLabel()
addTransactionValueLabel()
}
}
Run Code Online (Sandbox Code Playgroud)
extension String {
func strikedThrough() -> NSAttributedString {
let strikethroughStyle = [NSAttributedString.Key.strikethroughStyle: NSUnderlineStyle.single.rawValue]
return NSAttributedString(string: self, attributes: strikethroughStyle)
}
}
Run Code Online (Sandbox Code Playgroud)
我尝试了一些方法,但没有成功:
prepareForReuse并重置标签的text和attributedText。else后的if transactionData.isChargeback设置transactionDescriptionLabel和transactionValueLabel属性文本没有装饰那么,当单元格被重用时,如何重置它的标签以删除我之前添加的删除线样式?
Mar*_*aka 15
亚历克斯的回答确实解决了我的问题,但我也发现了为什么标签的重置不起作用。我会把它留在这里,以防它对某人有用。
出于某种原因,如果我只重置attributedText,它会起作用:
transactionDescriptionLabel.attributedText = nil
transactionValueLabel.attributedText = nil
Run Code Online (Sandbox Code Playgroud)
或者,如果我重置第attributedText一个,然后重置text,它也可以工作:
transactionDescriptionLabel.attributedText = nil
transactionValueLabel.attributedText = nil
transactionDescriptionLabel.text = nil
transactionValueLabel.text = nil
Run Code Online (Sandbox Code Playgroud)
根据UILabel文档,为任一属性分配新值会替换另一个属性的值,因此顺序无关紧要。但显然,确实如此。
https://developer.apple.com/documentation/uikit/uilabel/1620538-text
https://developer.apple.com/documentation/uikit/uilabel/1620542-attributedtext
您应该尝试在此处设置.attributedText而不是使用“.text”。如果不起作用我会删除我的答案。
// Fill in the values
transactionDescriptionLabel.text = transactionData.description
transactionValueLabel.text = Formatter.currency.string(for: transactionData.balance)
Run Code Online (Sandbox Code Playgroud)
所以,试试这个
transactionDescriptionLabel.attributedText = //
transactionValueLabel.attributedText = //
Run Code Online (Sandbox Code Playgroud)
还有一件事。其实我不喜欢didSet。我建议您创建一种方法来配置您的单元。这是我想告诉你的一个例子。
func configure(with transactionData: TimelineResponseModel) {
// Reset the labels
transactionDescriptionLabel.text = nil
transactionDescriptionLabel.attributedText = nil
transactionValueLabel.text = nil
transactionValueLabel.attributedText = nil
// Fill in the values
transactionDescriptionLabel.text = transactionData.description
transactionValueLabel.text = Formatter.currency.string(for: transactionData.balance)
if transactionData.isChargeback {
let value = Formatter.currency.string(for: transactionData.balance) ?? ""
transactionDescriptionLabel.attributedText = transactionData.description.strikedThrough()
transactionValueLabel.attributedText = value.strikedThrough()
}
}
Run Code Online (Sandbox Code Playgroud)
下一个。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: TimelineTableViewCell.reuseIdentifier,
for: indexPath) as? TimelineTableViewCell else { fatalError() }
// Thats what I really like
cell.configure(with: viewModel.timeline[indexPath.row])
return cell
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2126 次 |
| 最近记录: |