在ios8下运行模拟器时,我有一个tableview,显示为多行。在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
}
答案 0 :(得分:1)
在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和iOS中的tableView中显示多行。 7。