且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

快速更改表格视图单元格的大小

更新时间:2022-12-28 17:13:18

1)要更改所有具有相同类型的单元格的大小,请先您需要创建 CellType ,这是如何执行此操作的说明。
https://***.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://***.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
    }
}