I click on the row there will be a strikethrough, after clicking once I hope to remove the strikethrough
There is a completed degree default value is false
let newList = List(name: name, completed: false)
I tried removing the strikethrough with cell.listName.attributedText = nil, but it will disappear with my text
func completeList(_ indexPath: IndexPath) {
let lists = self.lists[indexPath.row]
self.lists[indexPath.row] = lists
if let cell = listTableView.cellForRow(at: indexPath) as? ListTableViewCell {
cell.listName.attributedText = strikeThroughText(lists.name!)
}
}
// 刪除線
func strikeThroughText(_ text: String) -> NSAttributedString {
let attributeString = NSMutableAttributedString(string: text)
attributeString.addAttribute(NSAttributedStringKey.strikethroughStyle, value: 2, range: NSMakeRange(0, attributeString.length))
attributeString.addAttribute(NSAttributedStringKey.strikethroughColor, value:UIColor(red: 235, green: 86, blue: 87), range: NSMakeRange(0, attributeString.length))
return attributeString
}
TableView Delegate , DataSource
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return lists.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let listCell = tableView.dequeueReusableCell(withIdentifier: "listCell") as! ListTableViewCell
let listItem = lists[indexPath.row]
listCell.listName.text = listItem.name
if listItem.completed {
listCell.listName.attributedText = strikeThroughText(listItem.name!)
}
listCell.backgroundColor = UIColor.clear
listCell.selectionStyle = .none
return listCell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
completeList(indexPath)
}
Try this:
let attributeString = NSMutableAttributedString(string: text)
attributeString.removeAttribute(NSAttributedStringKey.strikethroughStyle, range: NSMakeRange(0, attributeString.length))
attributeString.removeAttribute(NSAttributedStringKey.strikethroughColor, range: NSMakeRange(0, attributeString.length))