UITableViewCell`sizeToFit`中的UILabel无法通过AutoLayout在单元格内使用 [英] UILabel inside UITableViewCell `sizeToFit` not working inside cell with AutoLayout

查看:82
本文介绍了UITableViewCell`sizeToFit`中的UILabel无法通过AutoLayout在单元格内使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试解决这一问题将近一天了.因此,该点应与其旁边的UILabel对齐.关键是,第二个标签在填充正确的内容时有时会变成2行.点必须是与UILabel的第一行对齐,无论它是一行还是两行.

I've been trying to resolve this issue for almost a day. So the dot should align with the UILabel next to it. The thing is, the second label will sometimes become 2 lines when fill with the right content. The dot has to be align with the first line of the UILabel, whether it has 1 or two lines.

我尝试在更新单元格时在运行时调用sizeToFit().似乎没有效果.我阅读了在UILabel线程中将文本垂直对齐到顶部.

I tried to call sizeToFit() at runtime when the cell is updated. Seems no effect. I read Vertically align text to top within a UILabel thread.

我模拟sizeToFit()(编辑器->要适合内容的大小)是否成功调用,行的高度不会缩小.

I simulate if sizeToFit() (Editor -> Size to fit content) get called successfully, the height of the row will not shrink.

每行都包含UIView,此视图内有点和UILabel.点也是UIView.单行UILabel的行具有高度常数20.我可能将两行UILabel设置为< = 26.

Each row consist of UIView, inside this view there are the dot and the UILabel. The dot is UIView as well. The row with single line UILabel has height constant 20. The one with possibly 2 lines UILabel I set to <= 26.

点必须与UILabel的第一行对齐,无论它是1行还是2行.在较小的屏幕上效果很好.

The dot has to be align with the first line of the UILabel, whether it has 1 or two lines. This works well on smaller screen.

有人可以帮忙吗?

推荐答案

1. UIViewController层次结构

2.约束条件

  1. View Main Content BG

  • 顶部,底部,前导,尾随= 0 wrt超级观看.

lbl1

  • top = 10,trailling = 10 wrt superview

dot1

  • 开头= 10顶部= lbl1.top,水平间距wtl到lbl1 = 10,高度= 10宽度= 10

lbl2

  • 垂直空间wrt lbl1 = 10,前导&尾随= lbl1.前导& lbl1.leading
  • Verticle space wrt lbl1 = 10 , leading & trailling = lbl1.leading & lbl1.leading

dot2

  • Leading = dot1.leading,top = lbl2.top,height = 10 width = 10

lbl3

  • 垂直空间wrt lbl2 = 10,前导&尾随= lbl2.leader& lbl2.leading and bottom = 10 wrt superview
  • Verticle space wrt lbl2 = 10 , leading & trailling = lbl2.leading & lbl2.leading and bottom = 10 wrt superview

dot1

  • Leading = dot2.leading,top = lbl3.top,height = 10 width = 10

不要忘记让每个标签的行数保持为0.并绑定表视图委托&带有VC的数据源.

Dont forget to keep no of lines = 0 of every label. and bind the tableview delegate & datasource wrt VC.

3.现在,您的情节提要设计如下:

4.现在在VC中

虚拟数组

var arrList : [[String : String]] = [["lbl1": "Do any additional setup after loading the view, typically from a nib.",
                                          "lbl2": "Do any additional.",
                                          "lbl3": "that can be recreated"],
                                         ["lbl1": "Do any additional .",
                                          "lbl2": "Do any additional setup after loading the view, typically from a nib.",
                                          "lbl3": "that can be recreated"],
                                         ["lbl1": "Do any additional . Do any additional setup after loading the view, typically from a nib.",
                                          "lbl2": "Do any additional setup after loading the view, typically from a nib.",
                                          "lbl3": "that can be recreated Do any additional setup after loading the view, typically from a nib. Do any additional setup after loading the view, typically from a nib."]]

TableView委托&数据源

TableView delegates & datasource

func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
    return 110
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return UITableViewAutomaticDimension
}

public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
    return arrList.count
}


public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
    let cell = tableView.dequeueReusableCell(withIdentifier: "TblCell") as! TblCell

    let dict = arrList[indexPath.row]

    cell.lbl1.text = dict["lbl1"]
    cell.lbl2.text = dict["lbl2"]
    cell.lbl3.text = dict["lbl3"]

    return cell
}

5.最终输出

编辑

如果确实使用其respec标签将常数= 5 wr的点设置在顶部,则输出将在下面.

If you do set top of dots with constant = 5 wr with their respec labels then output is below.

这篇关于UITableViewCell`sizeToFit`中的UILabel无法通过AutoLayout在单元格内使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆