2016-08-16 36 views
3

我正在为列表制作一个视图。它具有动态的字幕长度。所以我使用了UITableViewAutomaticDimension属性。我没有应用任何约束,因为这两个标签都是表视图单元格属性,因此不需要创建新的自定义单元格。UITableViewAutomaticDimension应用于字幕表格视图单元格不能正常工作

但运行这个项目后,我标记了UITableViewAutomaticDimension不能正常工作。我不知道我做错了什么。我的代码是像下面

视图做负载

@IBOutlet weak var tblabc: UITableView! 
    override func viewDidLoad() { 
     super.viewDidLoad() 

     tblabc.separatorColor = UIColor.clearColor() 

     tblabc.estimatedRowHeight = 44 
     tblabc.rowHeight = UITableViewAutomaticDimension 



     // Do any additional setup after loading the view. 
    } 

表视图委托和数据源

func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
     return 1 
    } 
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return 5 
    } 
    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { 
     return UITableViewAutomaticDimension 
    } 
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell 
    { 
     var cell = tableView.dequeueReusableCellWithIdentifier("Cell") 
     if (cell == nil) 
     { 

      cell = UITableViewCell(style: .Subtitle, reuseIdentifier: "Cell") 
      cell?.selectionStyle = .None 
      cell?.detailTextLabel?.font = _SETREGULARFONT_KARLA(IS_IPAD ? 19:15) 
      cell?.textLabel?.font = _SETBOLDFONT_KARLA(IS_IPAD ? 17:13) 
      cell?.detailTextLabel?.textColor = getColorFromRGB(120, G: 132, B: 158, A: 1.0) 
      cell?.detailTextLabel?.numberOfLines = 0 
      cell?.detailTextLabel?.lineBreakMode = .ByTruncatingTail 
      cell?.selectionStyle = .None 

     } 

     cell?.textLabel?.text = "Testing" + "\(indexPath.row)" 
     cell?.detailTextLabel?.text = "tesfhjdsfskdfhjkfhdjskfhsjkdfhdjsxdfgfjljkgjfklgfhsdjfhjkdshfdjskfhdjfjkfhafyhdsifjhdjksfbshdjkfkhdksjfhdjsfyhds8ufdhsfjkhdsfjhdfudsiufhdsfh" 
     cell?.imageView?.image = UIImage(named: "new_temp") 

     return cell! 
    } 

查看看起来像下面的图片

enter image description here

+0

请参阅下面的链接http://stackoverflow.com/questions/36587126/autolayout-ignores-multi-line-detailtextlabel-when-calculating-uitableviewcell-h –

回答

1

选中此修改代码: -

func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
    return 1 
} 
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return 5 
} 
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell 
{ 
    var cell = tableView.dequeueReusableCellWithIdentifier("Cell") 
    if (cell == nil) 
    { 

     cell = UITableViewCell(style: .Subtitle, reuseIdentifier: "Cell") 
     cell?.selectionStyle = .None 
     cell?.detailTextLabel?.font = _SETREGULARFONT_KARLA(IS_IPAD ? 19:15) 
     cell?.textLabel?.font = _SETBOLDFONT_KARLA(IS_IPAD ? 17:13) 
     cell?.detailTextLabel?.textColor = getColorFromRGB(120, G: 132, B: 158, A: 1.0) 
     cell?.detailTextLabel?.numberOfLines = 0 
     cell?.detailTextLabel?.lineBreakMode = .ByTruncatingTail 
     cell?.selectionStyle = .None 

    } 

    cell?.textLabel?.text = "Testing" + "\(indexPath.row)" 
    cell?.detailTextLabel?.text = "tesfhjdsfskdfhjkfhdjskfhsjkdfhdjsxdfgfjljkgjfklgfhsdjfhjkdshfdjskfhdjfjkfhafyhdsifjhdjksfbshdjkfkhdksjfhdjsfyhds8ufdhsfjkhdsfjhdfudsiufhdsfh" 
    cell?.imageView?.image = UIImage(named: "new_temp") 

    return cell! 
} 

请删除HeightForRowAtIndex数据源方法。

-1

试试这个代码:

func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { 
     return UITableViewAutomaticDimension 
    } 

删除这两个行:

tblabc.estimatedRowHeight = 44 
tblabc.rowHeight = UITableViewAutomaticDimension 
+0

感谢您的快速回复,但这是行不通的 –

+0

The estimatedRowHeight不应该是UITableViewAutomaticDimension,它应该是一个实数,以帮助系统在自动布局确定动态行高度之前计算行高的估计值。 –

0

问题应固定在iOS版11.快捷的解决方案致力于降低就好了版本是通过设置其textlabel.attributedText具有更大胆的第一行(s)+ \ n +常规更多的行来在基本单元格内伪造期望的字幕外观。