如何在 UITableViewCell 中添加多行

How to Add multiple lines in UITableViewCell

我目前正在 Swift 开发一个应用程序,将数据存储到 table。目前我对正文下的字幕文本有疑问。我想在 "Licence Number" 参考图片下添加几行字幕。 I. 我已准备好一切,但不确定如何使其可见。任何帮助将不胜感激

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
    let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Value2, reuseIdentifier: "Nil")

    cell.textLabel!.text = LrnLog.logs[indexPath.row].name    //Trip Date- The data is used for the name
    cell.detailTextLabel!.text = LrnLog.logs[indexPath.row].desc    //Trip Name
    cell.detailTextLabel!.text = LrnLog.logs[indexPath.row].rego    //Vehicle Rego
    cell.detailTextLabel!.text = LrnLog.logs[indexPath.row].licnum  //License Number

    return cell
}

Image 2 is what my app is looking like

您正在更改标签的文本 3 次,而不是实际更改不同的标签。

您可以将所有这些字符串加在一起,并将它们放在一行中,或者您需要在单元格中放置 3 个单独的标签。同样在对您的问题发表评论之后,您可以将行数设置为 0,但您仍然必须将其组合成一个字符串。此外,我假设您也希望将数据放在该信息的右侧,在这种情况下,自定义单元格确实是您的最佳选择。

如果您决定创建一个自定义单元格,您还需要为您的单元格创建一个自定义 TableViewCell class,并且您可以将所有插座拖到它上面。

您应该在一个控制流中(或在一个代码块内)立即分配 text,您不能将不同的文本一个接一个地重新分配给相同的 属性。如果你这样做,那么编译器将考虑最新的而不是全部。因此,要回答您的问题,请在 cellForRowAtIndexPath:

中试试这个
cell.detailTextLabel!.numberOfLines = 0
cell.textLabel!.text = LrnLog.logs[indexPath.row].name
cell.detailTextLabel!.text = "\(LrnLog.logs[indexPath.row].desc)\n\(LrnLog.logs[indexPath.row].rego)\n\(LrnLog.logs[indexPath.row].licnum)"

假设LrnLog.logs[indexPath.row]中的上述属性none是nil,否则会崩溃。所以最好检查 nil 然后分配值。当字幕行变高时,您还需要调整单元格的大小。