ios - 重用 UITableViewCell 时无法重置 UILabel 属性文本
问题描述
问题
我正在使用 aUITableView
来显示信用卡的交易列表。如果交易是拒付,我将在标签中添加删除线样式:
当该特定单元被重用时,就会出现问题。即使重置标签的text
and属性,删除线装饰仍然存在。attributedText
下面我添加了我的代码的相关部分:
表视图
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
}
}
表格视图单元格
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()
}
}
删除线样式
extension String {
func strikedThrough() -> NSAttributedString {
let strikethroughStyle = [NSAttributedString.Key.strikethroughStyle: NSUnderlineStyle.single.rawValue]
return NSAttributedString(string: self, attributes: strikethroughStyle)
}
}
我试过的
我尝试了一些方法,但没有成功:
- 在那里覆盖
prepareForReuse
并重置标签的text
andattributedText
。 else
在之后创建一个if transactionData.isChargeback
以设置不带装饰的属性transactionDescriptionLabel
文本transactionValueLabel
那么,当单元格被重用时,如何重置它的标签以删除我之前添加的删除线样式?
解决方案
亚历克斯的回答确实解决了我的问题,但我也发现了为什么标签的重置不起作用。我会把它留在这里,以防它对某人有用。
出于某种原因,如果我只重置attributedText
,它可以工作:
transactionDescriptionLabel.attributedText = nil
transactionValueLabel.attributedText = nil
或者,如果我attributedText
先重置,然后重置text
,它也可以:
transactionDescriptionLabel.attributedText = nil
transactionValueLabel.attributedText = nil
transactionDescriptionLabel.text = nil
transactionValueLabel.text = nil
根据UILabel
文档,为任一属性分配新值会替换另一个属性的值,因此顺序无关紧要。但显然,确实如此。
https://developer.apple.com/documentation/uikit/uilabel/1620538-text
https://developer.apple.com/documentation/uikit/uilabel/1620542-attributedtext
推荐阅读
- docker - Docker 多实例 rethinkdb
- python - 在终端中更改命令名称
- reactjs - 如何设置复选框的最大数量
- recharts - 轴标签/刻度在 Recharts 中的位置
- r - 更改 Rmarkdown 中 DT:: 数据表中搜索框的默认大小
- xamarin - 从 Windows 10 笔记本电脑处理项目时出错
- python - UnicodeDecodeError:“ascii”编解码器无法解码位置 104 中的字节 0xef:序数不在范围内(128)
- codeigniter-3 - mpdf shows failed to load pdf document in all browsers
- android-studio - Android Studio 中的 Java 编译器错误
- node.js - Sequelize .count() 无法返回结果