gpt4 book ai didi

ios - 具有两个自动布局标签的 TableView 单元格,在 UIViewController 中不显示所有行

转载 作者:搜寻专家 更新时间:2023-10-31 22:49:09 24 4
gpt4 key购买 nike

我有一个带有两个 UILabel 的 UITableViewCell,使用 Storyboard 和 Autolayout。问题是第二个标签只显示两行文本,即使要显示更多行也是如此。

编辑:当 Controller 是 UIViewController 而不是 TableViewController 时会出现问题,它在 TableViewController 中按预期工作

我已将 numberoflines 属性设置为 0在自动布局中,我从所有四个侧面固定了两个标签。

但这并没有帮助,只显示了一行。

当我将 heightForRowAtIndexPath 与 systemLayoutSizeFittingSize 一起使用时 为了获得行的高度,事情略有改善并显示了两行。

TableViewCell with two labels

我在 8.2 和 8.4 上都测试过

我已经尝试过堆栈溢出 Multiple UILabels inside a self sizing UITableViewCell 中概述的解决方案

我已经尝试了本教程中概述的步骤:http://www.raywenderlich.com/73602/dynamic-table-view-cell-height-auto-layout

代码在这里:https://www.dropbox.com/sh/jp8ki0uxvfuf6ng/AABZlzzLnw9dMoamNbNkI_6Ba?dl=0

最佳答案

我是这样完成的:

为顶部标签创建前导、顶部和尾部约束为底部标签创建前导、底部和尾随约束

在两个标签之间创建一个垂直约束

确保您的尾随约束设置为使用单元格的大部分宽度

enter image description here

将 UILabel 的行数设置为 0

在您的代码中:

import UIKit

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

var textData = ["Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum euismod nisl neque, et eleifend felis dictum in. Nullam semper diam nec leo malesuada, non ultrices eros bibendum. Phasellus laoreet eros quis enim condimentum, nec pharetra augue ultrices. Nunc quis convallis magna. Fusce fringilla diam nunc, in consequat urna accumsan nec. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Cras in diam nec dolor tristique efficitur sed eu est.", "Maecenas ipsum nunc, ultricies non faucibus vitae, eleifend sit amet felis. Donec non dignissim sapien. Etiam vitae dapibus dolor. Duis nec diam sed augue aliquam vehicula. Interdum et malesuada fames ac ante ipsum primis in faucibus. Sed vulputate gravida tellus. Sed porttitor lacus ac venenatis fringilla. Pellentesque in sapien felis. Vestibulum pharetra fermentum arcu in sollicitudin. Phasellus convallis sit amet felis in rhoncus. Fusce consectetur tempus varius. Aliquam a fermentum dui. Curabitur vestibulum varius purus. Duis feugiat ligula a ligula imperdiet porta.","Quisque sagittis elementum odio vitae convallis. Nam dignissim pellentesque leo quis feugiat. Suspendisse a nisi et metus posuere dignissim sollicitudin nec lorem. Cras nec porta urna, sit amet bibendum dolor. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Maecenas enim dui, pellentesque in pulvinar non, faucibus sodales nibh. Mauris non condimentum ipsum.","Sed volutpat lectus id nibh scelerisque, ac mollis lectus cursus. Integer orci neque, euismod et dolor quis, condimentum pulvinar arcu. Etiam finibus nibh at dapibus fringilla. Proin viverra lobortis tempor. Vivamus luctus sem sed purus elementum, et tempor erat mollis. Duis cursus massa in mi imperdiet, condimentum semper est fringilla. Mauris a elit hendrerit, finibus felis sit amet, placerat nulla. Integer convallis massa vitae magna fermentum, id auctor neque varius. Aliquam vestibulum a lacus eu efficitur. Donec condimentum nunc eu ante vehicula aliquet. Vestibulum congue magna ut ultricies tempus. Phasellus porttitor at nisi sed tincidunt.","Curabitur lobortis non augue quis lacinia. In fermentum porta eleifend. Pellentesque eu ligula sit amet neque mollis varius vitae lacinia purus. Phasellus ut gravida elit. Maecenas id sem luctus, iaculis massa eget, condimentum mi. Vestibulum venenatis iaculis dignissim. Curabitur erat dui, eleifend ac turpis sit amet, viverra imperdiet metus. Integer eleifend a velit id bibendum."]

@IBOutlet weak var tableView: UITableView!

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.

tableView.estimatedRowHeight = 60 // The # from storyboard in the size inspector
tableView.rowHeight = UITableViewAutomaticDimension
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

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

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! CTableViewCell

cell.topLabel.text = ""
cell.bottomLabel.text = ""

cell.topLabel.text = "Cell Title"
cell.bottomLabel.text = textData[indexPath.row]

cell.bottomLabel.sizeToFit()

return cell

}

}

你会得到这个:

enter image description here

关于ios - 具有两个自动布局标签的 TableView 单元格,在 UIViewController 中不显示所有行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32357538/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com