是否可以在iOS 8中自行调整UITableViewCell的大小而无需创建自定义UITableViewCell?
我认为标准的UITableViewCell类型(UITableViewCellStyleDefault,UITableViewCellStyleSubtitle,UITableViewCellStyleValue1,UITableViewCellStyleValue2)内置了自动布局约束.事实证明,无法在Storyboard中更改非自定义单元格的约束.
但是当我使用UITableViewCellStyleValue1类型的非自定义单元格时,在Storyboard中设置它,将textOabel和detailTextLabel的numberOfRows设置为0,并将viewDidLoad代码设置为如下所示,在自动调整中仅考虑单元格的textLabel.细胞的高度.如果detailTextLabel最终显示在比textLabel更多的行上,则detailTextLabel的文本会溢出单元格的顶部和底部边缘.同样,单元格为textLabel正确调整大小,但似乎在其大小调整过程中忽略detailTextLabel.
我需要知道的主要事情是 – 如果我想要正确支持动态文本和自我调整大小,我是否需要为可以使用标准单元格的行创建自定义单元格?
- - (void)viewDidLoad {
- [super viewDidLoad];
- [self.tableView setEstimatedRowHeight:DEFAULT_ROW_HEIGHT];
- [self.tableView setRowHeight:UITableViewAutomaticDimension];
- }
解决方法
我刚刚在iOS 10 / XCode 8(iOS 9 / XCode 7中的结果相同)中使用不同的单元格类型进行了尝试,看起来它只适用于textLabel而不适用于detailTextLabel.
(基本上重复OP提到的问题)
ViewController代码,在detailTextLabel和textLabel上交替设置一些文本.
- class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
- @IBOutlet weak var tableView: UITableView!
- override func viewDidLoad() {
- super.viewDidLoad()
- tableView.estimatedRowHeight = 44
- tableView.rowHeight = UITableViewAutomaticDimension
- }
- func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
- let cell = tableView.dequeueReusableCell(withIdentifier: "Cell",for: indexPath)
- if indexPath.row % 2 == 0 {
- cell.textLabel?.text = "Lorem ipsum dolor sit amet,consectetur adipiscing elit,sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
- cell.detailTextLabel?.text = "<- textLabel"
- } else {
- cell.textLabel?.text = "detailTextLabel ->"
- cell.detailTextLabel?.text = "Lorem ipsum dolor sit amet,sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
- }
- return cell
- }
- func tableView(_ tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
- return 10
- }
- }
确保将textLabel和textDetailLabel的line属性设置为0,这是结果.
我会将此报告为错误.