无法使字符串分成几行。我正在传递MainViewController中的数据以在单个行表中显示。我尝试将对象产品与Product类一起传递,因此将产品描述作为product.description获得。我也尝试过只传递String,但是无论哪种方式,我都会得到图像中显示的结果。这些描述来自Firestore数据库,该数据库存储为例如:字符串黑色和白色可用\ n一件泳衣\ nScoop脖子\ nCheeky Coverage \ n背带细节\ n90%尼龙10%氨纶\ n最终销售
我尝试了几种方法:
var product: Product?
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "description", for: indexPath) as! ProductDescriptionTableViewCell
cell.descriptionLabel.text = product?.description
return cell
}
或在viewDidLoad()中加载描述
var product: Product?
var productDescription = String()
override func viewDidLoad() {
super.viewDidLoad()
productDescription = product!.description
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "description", for: indexPath) as! ProductDescriptionTableViewCell
cell.descriptionLabel.text = productDescription
return cell
}
我仍然无法使用“ \ n”获得所需的字符串中断结果。我对其进行了测试,它可以使用以下命令直接添加文本:
cell.descriptionLabel.text = "Available In Black And White\nOne Piece Swimsuit\nScoop Neck\nCheeky Coverage\nStrappy Detail At Back\n90% Nylon 10% Spandex\nFinal Sale"
所以我知道单元格的高度和标签的行数有效。