ios tableview 仅将单元格显示为多行 ios8 而不是 ios7
ios tableview displays cell as multiline only ios8 not ios7
当 运行 模拟器在 ios8 下时,我有一个按预期显示为多行的表格视图。当 运行 在 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=0
和 heightForRowAtIndexPath
,我能够在 iOS 8 和 7 的 tableView 中显示多行。
当 运行 模拟器在 ios8 下时,我有一个按预期显示为多行的表格视图。当 运行 在 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=0
和 heightForRowAtIndexPath
,我能够在 iOS 8 和 7 的 tableView 中显示多行。