【问题标题】:UILabel doesn't do multiline in a custom UITableViewCell on launch but does after scrolled-backUILabel 在启动时不会在自定义 UITableViewCell 中执行多行,但在回滚后会执行
【发布时间】:2015-09-22 11:49:37
【问题描述】:

编辑#1

我在 github 上添加了一个项目链接: https://github.com/trestles/testtable

这真的是我第一次处理 Autolayout,所以我预计我会犯一些业余错误。老实说,我知道如何操作帧,但无法通过内容剪辑的自动布局正常工作。部分问题是,如果我们始终处于纵向模式,我应该只使用框架吗?


我有一个自定义 UITableViewCell,其中有几个 UILabel。它们设置为 numberOfLines=0。有时,他们会截断文本。像这样:

我该如何解决这个问题?我试图在 viewDidLoad 中重新加载数据,但这似乎并不重要。大多数时候,当您滚动时,它会自行修复(但并非总是如此)。它可以是 UILabel 中的任意三个,并且与文本量无关。我第一次使用带有自动布局的 UILabels,所以很可能我犯了一些错误。这是我的 UILabel 属性:

以及第一个标签的布局:

【问题讨论】:

  • 你是否添加了'\n'来标记行尾
  • 我的理解是它应该换行并且 \n 是不必要的。我已经通过约束设置了宽度。我已经使用手动框架做了很多次,通常通过设置 numberOfLines=0 启用多线
  • 在此处粘贴一些代码。
  • 代码真的不多。我正在尝试通过 IB 配置标签。我已经包含了约束的屏幕截图。
  • 您的视图控制器显示一个红色箭头,表示您的约束存在问题。先解决这些问题,然后看看问题是否消失。另外,我建议不要在标签上设置宽度约束,而是为超级视图或相邻标签设置前导/尾随约束。

标签: ios autolayout uilabel


【解决方案1】:

我看到你只在高度上设置了约束,没有在宽度上。 也尝试设置它。

【讨论】:

    【解决方案2】:

    您的自动布局 完美,只是因为您在xib默认文本 设置auto layout 而出现了问题/强>。在您viewDidLoad 中,您正在更新UILabel 文本,但没有以编程方式更新layout。所以只剩下一行如下。

    self.mainTV.layoutIfNeeded();
    

    viewDidLoad 方法中重新加载UITableView 之前添加以上行。一切正常。

    示例

    override func viewDidLoad() {
        super.viewDidLoad()
    
        self.mainTV.dataSource=self
        self.mainTV.delegate=self
        self.mainTV.rowHeight = UITableViewAutomaticDimension
        self.mainTV.estimatedRowHeight = 84.0
        //self.mainTV.registerClass(MyTableViewCell.self, forCellReuseIdentifier: "MyCustomCell")
    
    
        var tmps = [String]()
    
        tmps.append("ABC But here is an artist. He desires to paint you the dreamiest, shadiest, quietest, most enchanting bit of romantic landscape in all the valley of the Saco. What is the chief element he employs?")
        tmps.append("DEF But here is an artist.")
        tmps.append("GHI But here is an artist. He desires to paint you the dreamiest, shadiest, quietest, most enchanting bit")
    
        for var i=0; i<10; i++
        {
          var menuItem=EKMenuItem()
          let randomIndex1 = Int(arc4random_uniform(UInt32(tmps.count)))
          menuItem.header = "\(i) \(tmps[randomIndex1])"
    
          let randomIndex2 = Int(arc4random_uniform(UInt32(tmps.count)))
          menuItem.detail = "\(i) \(tmps[randomIndex2])"
          let randomIndex3 = Int(arc4random_uniform(UInt32(tmps.count)))
          menuItem.price = "\(i) \(tmps[randomIndex3])"
          //tmpItem.price = "my price"
          dataItems.append(menuItem)
        }
    
        self.view.backgroundColor = UIColor.greenColor()
    
        self.mainTV.layoutIfNeeded();
        self.mainTV.reloadData()
        // Do any additional setup after loading the view, typically from a nib.
      }
    

    希望对您有所帮助。

    【讨论】:

    • @timpone,如果您还有问题,请告诉我。
    【解决方案3】:

    我已经从 git 下载了源代码。我不确定这是否是错误。但是在 viewdidappear 中重新加载表格解决了这个问题。

    override func viewDidAppear(animated: Bool) {
    
            mainTV.reloadData()
    
        }
    

    这是执行上述操作时模拟器上的样子。

    【讨论】:

      【解决方案4】:

      只需在viewDidLayoutSubviews() 中添加self.mainTV.reloadData() 即可解决您的问题。

      您可以在此处查看更多详细信息。

      http://useyourloaf.com/blog/2014/08/07/self-sizing-table-view-cells.html

      【讨论】:

        【解决方案5】:

        更新您的单元格约束和布局

            func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
            let cell = tableView.dequeueReusableCellWithIdentifier("MyCustomCell") as! MyTableViewCell
            cell.headerLabel.text=dataItems[indexPath.row].header
            cell.setHeader(dataItems[indexPath.row].header)
            cell.setDetail(dataItems[indexPath.row].detail)
            cell.setPrice(dataItems[indexPath.row].price)
            // update constraint and layout
            cell.layoutIfNeeded()
            return cell
          }
        

        【讨论】:

          猜你喜欢
          • 2019-01-02
          • 1970-01-01
          • 2018-01-26
          • 2022-11-01
          • 2022-11-03
          • 1970-01-01
          • 2018-07-15
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多