uitableview - ios tableview 仅将单元格显示为多行 ios8 而不是 ios7

标签 uitableview swift multiline

在 ios8 下运行模拟器时,我有一个表格 View ,按预期显示为多行。在 ios7 下运行时,它将所有单元格限制为 2 行。我在网上发现很多地方的建议是使用 numberOfLines = 0 ,如下所示,但这没有任何作用。还尝试了 sizeToFit 和 lineBreakMode 但它们没有效果。有任何想法吗?创建自定义单元似乎有点矫枉过正,但我​​需要这样做吗?

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell

    // Configure the cell...
    //cell.textLabel?.lineBreakMode = .ByWordWrapping
    //cell.textLabel?.sizeToFit()
    cell.textLabel?.numberOfLines = 0;
    cell.textLabel?.text = "\(formattedPlateComments[indexPath.row])"

    return cell
}

最佳答案

在 Label 实例上设置 numberOfLines=0 是正确的,因为这指定了无限数量的行。但是,为了兼容 iOS 7,您还需要定义 heightForRowAtIndexPath,即:

func tableView(tableView: UITableView!, heightForRowAtIndexPath indexPath: NSIndexPath!) -> CGFloat {
    return 20    // you can also programmatically calculate a desired height
}

使用 numberOfLines=0heightForRowAtIndexPath,我能够在 iOS 8 和 7 中的 tableView 中显示多行。

关于uitableview - ios tableview 仅将单元格显示为多行 ios8 而不是 ios7,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29504611/

相关文章:

swift - 如何删除表格 View 单元格中的特定行?

ios - UITextView 和单元格根据 textview 的内容动态更新高度?

ios - Swift 中的子类错误 "EXC BAD ACCESS"

python - 匹配任意分成多行的字符串

vim - 如何使用 sed 或 vim 将 {{{...}}} 转换为 Markdown 代码块?

ios - 使用 tableViewCells 进行 Segue

iphone - 如何设置 UITableView 中文本的格式?

objective-c - 将 cvMat 转换为 UIImage 时出现内存问题

if 语句出现 Swift fatal error

javascript - 使用 d3js 绘制简单的多线图