ios – UILabel在启动时不在自定义UITableViewCell中执行多行,但在滚动后执行

编辑#1

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

这是我第一次与Autolayout打交道,所以我希望我会做一些业余错误.老实说,我知道我是如何做这个操纵帧,但无法通过内容裁剪自动布局正常工作.部分问题是,如果我们总是处于纵向模式,我应该使用框架吗?

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

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

和第一个标签的布局:

解决方法

您的自动布局非常完美,只是因为您在xib中使用认文本设置自动布局而出现问题.并在您viewDidLoad中,您正在更新UILabel文本但不以编程方式更新布局.所以只留下一行如下.
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,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.
  }

希望这对你有所帮助.

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...