快速更改表格视图单元格的大小 [英] Swift Changing the size of tableview cells

查看:75
本文介绍了快速更改表格视图单元格的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

就像标题中所说的那样,我正在寻找一种方法来更改表格视图中每个单元格的大小。我有2个问题:

just like the title says I'm searching for a way to change the size of each cell in my tableview. I have 2 questions:

1)如何在表格视图中以相同的数量更改所有单元格的大小?

1) How can I change the size of all cells by the same amount in my tableview?

2)是否可以根据特定单元格中文本的长度自动调整单元格的大小?

2) Is it possible to automatically adjust the size of the cell based on the length of the text in that particular cell?

我知道之前已经有类似的问题,但是由于它们已经过时了一段时间,因此我无法正确实施它们的答案。感谢您的帮助,谢谢您!

I'm aware that there are already similar questions asked before but I couldn't manage to implement their answers correctly as they've been outdated for some time now. I'd appreciate any help, thank you in advance!

推荐答案

1)要更改所有具有相同类型的单元格的大小,请先您需要创建 CellType ,这是如何执行此操作的说明。
https://stackoverflow.com/a/51979506/8417137
或您可以检查indexPath.row,但不是很酷:)

1) To change size of all cells with same type, first you need to create CellType, here is the explanation how to do this. https://stackoverflow.com/a/51979506/8417137 or you can check for indexPath.row, but it's not cool :)

比这里:

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    switch cellType {
    case firstType:
      return firstCellTypeHeight
    case secondType: 
      return secondCellTypeHeight
    default:
      return defaultCellHeight
    }
}

2)是的,可能。您创建 CustomCell 。在 CustomCell 中,您应该
ContentView 内设置textField或标签。您对文本的看法将扩大您的单元格。

2) Yes, its possible. You create your CustomCell. In your CustomCell you should setup your textFields or labels inside ContentView. Your views with text will stretch your cell.

重要的事情。在上面的方法中,您必须这样返回特殊高度。

Important thing. In method above you must return special height like that.

return UITableViewAutomaticDimension

例如,您的代码看起来像这样。

For example your code will looks like that.

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    switch cellType {
    case firstType:
      return firstCellTypeHeight
    case secondType: 
      return secondCellTypeHeight
    // Your stretching cell
    case textCellType:
      return UITableViewAutomaticDimension
    }
}

override func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
    switch cellType {
    case firstType:
      return firstCellTypeHeight
    case secondType: 
      return secondCellTypeHeight
    // Your stretching cell
    case textCellType:
      return UITableViewAutomaticDimension
    }
}

这篇关于快速更改表格视图单元格的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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