静态UITableView,在Swift中创建一个具有动态高度的单个单元格

时间:2023-01-22 22:58:36

I have a static UITableView which has 12 rows and for 11 rows I know what the height needs to be.

我有一个静态UITableView有12行,11行我知道高度需要是什么。

I have a UILabel which has dynamic text which sits inside the 12 row, how do i go about making just that one cell to have a dynamic height based on the text in the UILabel.

我有一个UILabel,它有动态文本,位于12行内,我如何根据UILabel中的文本制作一个单元格的动态高度。

I have tried the below code inside viewDidLoad, but it didn't work. The rows remained the same height. I have also set the lines = 0 for the UILabel

我在viewDidLoad中尝试了以下代码,但它没有用。行保持相同的高度。我还为UILabel设置了lines = 0

    tableView.estimatedRowHeight = 100.0
    tableView.rowHeight = UITableViewAutomaticDimension

6 个解决方案

#1


9  

Have you tried this?

你试过这个吗?

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    if (indexPath.row < 11) {
        // Everything starts at 0, so this covers 0-10
        // Whatever your cell height is
        return <#fixedCellHeight#>
    } else {
        // This is your 12th cell (indexPath.row 11), as we start counting at 0
        return UITableViewAutomaticDimension
    }
}

#2


1  

Try this:

尝试这个:

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

#3


0  

You can create 2 cell prototype cells for the table view. You give them 2 different id. And then in your code you override this fonction

您可以为表视图创建2个单元格原型单元格。你给他们2个不同的身份证。然后在你的代码中覆盖这个功能

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

    var cell = UITableViewCell()

    if indexPath.row < 12 {
        cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
    } else {
        cell = tableView.dequeueReusableCellWithIdentifier("cell12", forIndexPath: indexPath)
    }

    return cell
}

#4


0  

You Can create the dynamic cell with Text height then you can set the static and dynamic cell both in on table view

您可以使用文本高度创建动态单元格,然后可以在表格视图中设置静态和动态单元格

Here is link to dynamic cell with text How to change cell height dynamically in UITableView static cell

以下是带有文本的动态单元格的链接如何在UITableView静态单元格中动态更改单元格高度

#5


0  

Adding to @Adrian answer , if you are using static Cells , changing one cell to dynamic height , and others as they are you can edit it to this .

添加到@Adrian答案,如果您使用的是静态单元格,将一个单元格更改为动态高度,而其他单元格则可以将其编辑为此。

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

        if indexPath.row == 11 {
      // This is your 12th cell (indexPath.row 11), as we start counting at 0
            return UITableViewAutomaticDimension
        } else {
            return super.tableView(tableView, heightForRowAtIndexPath: indexPath)
        }
     }

#6


0  

More elegant might be to just implement as suggested:

更优雅的可能是按照建议实施:

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

Or in Objective C:

或者在目标C中:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewAutomaticDimension;
}

But rather than the row specific logic, add constraints to your static cell's content in the Storyboard to keep the row heights constant. That way if you move rows or change content you don't need to change any code.

但是,不是特定于行的逻辑,而是在Storyboard中为静态单元格的内容添加约束,以保持行高不变。这样,如果您移动行或更改内容,则无需更改任何代码。

#1


9  

Have you tried this?

你试过这个吗?

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    if (indexPath.row < 11) {
        // Everything starts at 0, so this covers 0-10
        // Whatever your cell height is
        return <#fixedCellHeight#>
    } else {
        // This is your 12th cell (indexPath.row 11), as we start counting at 0
        return UITableViewAutomaticDimension
    }
}

#2


1  

Try this:

尝试这个:

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

#3


0  

You can create 2 cell prototype cells for the table view. You give them 2 different id. And then in your code you override this fonction

您可以为表视图创建2个单元格原型单元格。你给他们2个不同的身份证。然后在你的代码中覆盖这个功能

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

    var cell = UITableViewCell()

    if indexPath.row < 12 {
        cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
    } else {
        cell = tableView.dequeueReusableCellWithIdentifier("cell12", forIndexPath: indexPath)
    }

    return cell
}

#4


0  

You Can create the dynamic cell with Text height then you can set the static and dynamic cell both in on table view

您可以使用文本高度创建动态单元格,然后可以在表格视图中设置静态和动态单元格

Here is link to dynamic cell with text How to change cell height dynamically in UITableView static cell

以下是带有文本的动态单元格的链接如何在UITableView静态单元格中动态更改单元格高度

#5


0  

Adding to @Adrian answer , if you are using static Cells , changing one cell to dynamic height , and others as they are you can edit it to this .

添加到@Adrian答案,如果您使用的是静态单元格,将一个单元格更改为动态高度,而其他单元格则可以将其编辑为此。

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

        if indexPath.row == 11 {
      // This is your 12th cell (indexPath.row 11), as we start counting at 0
            return UITableViewAutomaticDimension
        } else {
            return super.tableView(tableView, heightForRowAtIndexPath: indexPath)
        }
     }

#6


0  

More elegant might be to just implement as suggested:

更优雅的可能是按照建议实施:

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

Or in Objective C:

或者在目标C中:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewAutomaticDimension;
}

But rather than the row specific logic, add constraints to your static cell's content in the Storyboard to keep the row heights constant. That way if you move rows or change content you don't need to change any code.

但是,不是特定于行的逻辑,而是在Storyboard中为静态单元格的内容添加约束,以保持行高不变。这样,如果您移动行或更改内容,则无需更改任何代码。