Tableview - 标题重叠细节(Swift)

时间:2018-04-16 09:31:19

标签: ios swift xcode uitableview

附上的图片说明了一切。有没有办法防止标题重叠,例如每当达到限制时自动移动到第二行?以编程方式/通过接口构建器。

P.S:我从firebase数据库中检索名称。这是一个投票页面,标题是展位名称

TableView控制器

<jboss xmlns="urn:jboss:1.0">
   <batch xmlns="urn:jboss:batch-jberet:1.0">
      <thread-pool name="testpool"/>
   </batch>
</jboss>

enter image description here

5 个答案:

答案 0 :(得分:0)

您需要将行设置为0:

enter image description here

您可以直接从StoryBoard设置:

或者您可以设置编程:

cell.textLabel?.numberOfLines = 0
cell.detailTextLabel?.numberOfLines = 0

希望得到这个帮助。

编辑:TableView约束的演示

我在单元格中取了两个标签:

这是我的TableView输出:

enter image description here

这是标签First的约束:

enter image description here

这是标签2的约束:

enter image description here

是的,它对我有用,文字是多行的,没有文字切割,请仔细检查你的约束。

编辑查看约束

选择标签,然后选择显示尺寸检查器:

向下滚动,您可以看到给定的约束。

enter image description here

编辑2

根据您的描述,您有自动调整大小:

所以你可以使用以下内容:

对于标签一

enter image description here

标签第二:

enter image description here

答案 1 :(得分:0)

由于您使用默认的UITableViewCell&#39; textLabel ,这应该可以解决所有问题:

cell.textLabel.numberOfLines = 0;
cell.textLabel.lineBreakMode = .byWordWrapping;  

答案 2 :(得分:0)

您设置 UILabel 属性 numberOfLines = 0

enter image description here

  1. 设置 UILabel 顶部,底部,前导,尾随约束
  2. enter image description here

    1. 添加 UITableViewDelegate 方法。

      func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
          return UITableViewAutomaticDimension
      }
      
    2. 还可以设置投票限制

    3. enter image description here

      1. 选择投票UIButton 设置约束

        1. 步骤设置顶部,底部,前导,尾随和固定宽度。
      2. enter image description here

        1. 步骤设置垂直居中约束
        2. enter image description here

答案 3 :(得分:0)

确保三件事: -

  • 标题标签具有底部,顶部和前导约束,具有单元格内容视图以及具有投票&gt;的水平间距,并且投票&gt;按钮具有所有约束,如顶部前导和带标题的水平空间。
  • 标题标签已设置numberOfLines = 0
  • 使用heightForRowAt中的UITableViewAutomaticDimension设置单元格的动态高度

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

答案 4 :(得分:0)

试试代码

// in view did load:
self.tableView.rowHeight = UITableViewAutomaticDimension

// cell row at indexpath
let reuseIdentifier = "cell"
        var cell:UITableViewCell? =
            tableView.dequeueReusableCell(withIdentifier: reuseIdentifier)
        if (cell == nil)
        {
            cell = UITableViewCell(style: .value1,
                                   reuseIdentifier: reuseIdentifier)
        }

        // your customize font, color... here

        cell!.textLabel?.numberOfLines = 0;
        cell!.detailTextLabel?.numberOfLines = 0;

        return cell!