【发布时间】:2021-11-14 13:31:46
【问题描述】:
这是一个简单的测试代码来演示我遇到的问题。当 tableView 首次出现时,它的所有行在单元格的默认 textLabel 的文本中都有一个删除线,使用属性字符串。当我单击一行时,它应该删除删除线属性,但属性文本保持不变。我做错了什么?
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var tableData = ["Green tomatoes", "Yellow bananas", "Red peppers"]
let cellId = "cellID"
override func viewDidLoad() {
super.viewDidLoad()
let tableview = UITableView(frame: CGRect(x: 0, y: 200, width: view.frame.width, height: view.frame.height))
tableview.register(UITableViewCell.self, forCellReuseIdentifier: cellId)
tableview.rowHeight = 40
tableview.delegate = self
tableview.dataSource = self
view.addSubview(tableview)
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return tableData.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath)
cell.backgroundColor = #colorLiteral(red: 0.7176730633, green: 0.9274803996, blue: 0.9678700566, alpha: 1)
cell.textLabel?.attributedText = tableData[indexPath.row].addStrikeThrough()
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath)
cell.textLabel?.attributedText = tableData[indexPath.row].removeStrikeThrough() // THIS DOESN'T WORK
tableView.reloadData() // This doesn't make any difference
}
}
extension String {
func addStrikeThrough() -> NSAttributedString {
let attributeString = NSMutableAttributedString(string: self)
attributeString.addAttribute(NSAttributedString.Key.strikethroughStyle,
value: 2,
range: NSRange(location: 0, length: attributeString.length))
attributeString.addAttribute(NSAttributedString.Key.strikethroughColor,
value: UIColor.red,
range: NSMakeRange(0, attributeString.length))
return attributeString
}
func removeStrikeThrough() -> NSAttributedString {
let attributeString = NSMutableAttributedString(string: self)
attributeString.removeAttribute(NSAttributedString.Key.strikethroughStyle, range: NSMakeRange(0, attributeString.length))
return attributeString
}
}
但是,它工作对于常规的 UILabel 来说还不错:
class ViewController: UIViewController {
var label: UILabel!
let labelText = "Hello World"
override func viewDidLoad() {
label = UILabel()
label.attributedText = labelText.addStrikeThrough()
label.backgroundColor = .yellow
label.isUserInteractionEnabled = true
label.translatesAutoresizingMaskIntoConstraints = false
let tap = UITapGestureRecognizer(target: self, action: #selector(labelTapped))
label.addGestureRecognizer(tap)
view.addSubview(label)
NSLayoutConstraint.activate([
label.centerXAnchor.constraint(equalTo: view.centerXAnchor),
label.topAnchor.constraint(equalTo: view.topAnchor, constant: 200),
label.heightAnchor.constraint(equalToConstant: 30)
])
}
@objc func labelTapped() {
label.attributedText = labelText.removeStrikeThrough() // THIS WORKS!
}
}
extension String {
func addStrikeThrough() -> NSAttributedString {
let attributeString = NSMutableAttributedString(string: self)
attributeString.addAttribute(NSAttributedString.Key.strikethroughStyle,
value: 2,
range: NSRange(location: 0, length: attributeString.length))
attributeString.addAttribute(NSAttributedString.Key.strikethroughColor,
value: UIColor.red,
range: NSMakeRange(0, attributeString.length))
return attributeString
}
func removeStrikeThrough() -> NSAttributedString {
let attributeString = NSMutableAttributedString(string: self)
attributeString.removeAttribute(NSAttributedString.Key.strikethroughStyle, range: NSMakeRange(0, attributeString.length))
return attributeString
}
}
【问题讨论】:
-
我没有看到非常重要的事情 - 你如何刷新表格。你在哪里更新
tableData数组?我看到您正在更新选定的单元格内容,但UITableView基于数据源(在您的情况下为tableData)并且它没有被更新。您可能需要 (1) 向此数组添加一个字段,声明是否使用删除线文本,(2) 初始化它以执行此操作,然后 (3) 更新该数组以指示指向dataSource的单元格被选中,并且最后(4)致电reloadData。
标签: ios swift uitableview nsattributedstring