如何使用Swift创建具有动态单元格高度的静态单元格 [英] How to create static cells with dynamic cell heights with Swift

查看:145
本文介绍了如何使用Swift创建具有动态单元格高度的静态单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看过几本教程,这些教程显示了如何设置动态像元高度,但是只有在您通过设置适当的约束并使用UITableViewAutomaticDimension使用动态像元时,所有这些教程才显示出来.但是,我想对静态单元格执行此操作.

I have looked at several tutorials that show how to set dynamic cell heights, but all of them only show it if you are using dynamic cells by setting the appropriate constraints and using UITableViewAutomaticDimension. However, I would like to do this for static cells.

我的应用程序中有一个表视图控制器,其中包含几个单元格,这些单元格显示带有公开指示符的类别,该指示符中有一个标题,然后描述和描述文本的大小会有所不同.因此,我希望单元格的高度根据描述文本的大小是动态的.

I have a table view controller in my app that contains several cells that display a category with a disclosure indicator in which there is a title and then a description and the size of the description text varies. Thus, I would like the height of the cell to be dynamic per the size of the description text.

之所以使用静态单元格而不是动态单元格,是因为在类别单元格上方,我有一些具有某些设计的单元格,而这些设计只能通过使用静态单元格和情节提要来实现.

The reason why I am using static cells and not dynamic cells is because above the category cells I have a few cells with some designs that I could only get by using static cells and storyboarding.

我正在使用iOS9Xcode 7.3Swift 2.2

更新

表视图控制器代码

Table View Controller Code

import UIKit

class CategoriesTableViewController: UITableViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.rowHeight = UITableViewAutomaticDimension
        tableView.estimatedRowHeight = 140
    }

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

    // MARK: - Table view data source

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return 2
    }

}

更新2

下面每个接受的答案,我在表视图控制器中添加了以下两种方法,并删除了viewDidLoad()中的两行.这为我做到了!请记住,我必须确保每个单元格中的所有标签都使用顶部,底部,前导和尾随约束.另外,每个标签的行数必须设置为0.

Per accepted answer below I added the two methods below to my table view controller and removed the 2 lines I had in viewDidLoad(). This did it for me! Keep in mind that I had to make sure all the labels in each cell used a top, bottom, leading and trailing constraint. Also, the number of lines for each label must be set to 0.

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return UITableViewAutomaticDimension
}

override func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return UITableViewAutomaticDimension
}

推荐答案

UILabel:

通过编程设置numberOfLines = 0或在UILabel的属性检查器中将行设置为零.

Set numberOfLines = 0 via programmatically or set lines to zero in attributes inspector of the UILabel.

UITextView:

让我们选择您的文本视图,然后取消选中启用的滚动,如下图所示.

Let's select your text view and uncheck the scrolling enabled like as below image.

将以下代码添加到您的视图控制器中:

Add the below code to your view controller:

快捷键5:

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

func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
    return UITableView.automaticDimension
}

Swift 2.2:

 func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
   return UITableViewAutomaticDimension
 }

 func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
   return UITableViewAutomaticDimension
 }

这篇关于如何使用Swift创建具有动态单元格高度的静态单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆