ios - swift 4 删除删除线

标签 ios swift uitableview nsattributedstring

我点击该行会有删除线,点击一次后我希望去掉删除线

有完成度默认值为false

let newList = List(名称: 名称, 完成: false)

我尝试删除删除线 cell.listName.attributedText = nil, 但它会随着我的文字消失

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)、数据源

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)

}

最佳答案

试试这个:

let attributeString = NSMutableAttributedString(string: text)
attributeString.removeAttribute(NSAttributedStringKey.strikethroughStyle, range: NSMakeRange(0, attributeString.length))
attributeString.removeAttribute(NSAttributedStringKey.strikethroughColor, range: NSMakeRange(0, attributeString.length))

关于ios - swift 4 删除删除线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50793691/

相关文章:

ios - 如何在 UITableView 编辑模式下更改左侧图标

ios - 如何在我的 iOS 应用程序中导入和读取文件?

android - 如何在 android 中为 gridview(或表格 View )框添加轮廓

objective-c - 如何在 Xcode 中为进度条创建平滑的动画

ios - 自定义导航栏按钮项目上的阴影裁剪到边界

ios - 将整数发送回前一个 View Controller

swift - UINavigationController 导航栏不应用透明图像

ios - 如何为 tableView Section 的第一个和最后一个单元格设置圆角

ios - 使用 NSURLSession 时无法调用 didReceiveData

ios - 当应用程序未运行时处理 iOS 推送通知