【发布时间】:2019-05-29 08:49:37
【问题描述】:
我拥有动态数据,我将该数据加载到 tableview 中,但这些数据混合了属性字符串和普通字符串。
在这里我得到“点击这里”,我想用蓝色扩大那个字符串并选择它需要打开网址。
我写了以下代码,但它给出了错误。
二元运算符“+”不能应用于“字符串”类型的操作数和 'NSMutableAttributedString'
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "WhatisrewardsTableViewCell", for: indexPath)as! WhatisrewardsTableViewCell
cell.imageview.image = images[indexPath.section][indexPath.row]
cell.titlelbl.text = tilenames[indexPath.section][indexPath.row]
cell.desclbl.text = descriptions[indexPath.section][indexPath.row]
let value = " here.".withTextColor(UIColor.disSatifyclr)
if (cell.desclbl.text?.contains("tap"))!{
// cell.desclbl.text?.attributedString
cell.desclbl.text = descriptions[indexPath.section][indexPath.row] + value
}
cell.desclbl.addLineSpacing()
return cell
}
【问题讨论】:
-
请分享你写到现在的代码。
-
在此处添加不同颜色后,我正在尝试获取所有单元格中的水龙头
-
let value = " here.".withTextColor(UIColor.disSatifyclr) if (cell.desclbl.text?.contains("tap"))!{ // cell.desclbl.text?.attributedString cell.desclbl.text = 描述[indexPath.section][indexPath.row] + 值}
-
您可以准备方法,该方法以动态字符串为参数并根据您的要求返回属性字符串,您必须使用该属性字符串到 cell.desclbl.text?.attributedString 对象
-
不相关但不使用多个数组作为数据源。这是非常糟糕的做法,而且很容易出错。使用自定义结构或类。
标签: ios swift tableview nsattributedstring