How to have multiple cell sizes in a dynamic tableView in swift?

I want to have cells with different sizes in the tableView, but everything after building and running the application, I see that all the cells are the same size, and the big cell is cropped and its contents become incomplete. I set the dimensions in the attribute inspector.

this is a screenshot

+4
source share
3 answers

it is as simple as the implementation:

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return CGFloat(#your desired size#)
}

just use the / case switch in this function and work with indexPath.row. You can also use UITableViewAutomaticDimensionto automatically calibrate

EDIT:

, . Rikh , , .

, , , . :

1: 2 , ( - type1, - type2).

if switch/case , , (, 88 , - 44 ):

return indexPath.section == 0 ? CGFloat(88.0) : CGFloat(44.0)

2: - , - .

, , viewDidLoad tableView.estimatedRowHeight = CGFloat(44.0), RowHeight . :

return indexPath.section == 0 ? CGFloat(88.0) : UITableViewAutomaticDimension

3: , , . , . , , . viewDidLoad ( 44 ):

tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = CGFloat(44.0)

, , . : , "detail" - , , , , "detail": ( > =) 21, ( " " ), "title", , 0, , , , " ". .

P.S.: , , , "title" . TableView , , " " " " , (CHCR)

+2

https://www.raywenderlich.com/129059/self-sizing-table-view-cells

  • , ?
  • contentView?
  • tableView.rowHeight = UITableViewAutomaticDimension?
0

Define auto-layout constraints for your prototype cell.

Indicate the rated RowHeight of your table view

Set rowHeight of your table view to UITableViewAutomaticDimension

Example

tableView.estimatedRowHeight = 44.0
tableView.rowHeight = UITableViewAutomaticDimension

If you use Autolayout, everything will work as expected.

You will receive a sample demo here.

https://www.dropbox.com/s/ug9xu0yfcua902o/SelfSizingDemo.zip?dl=0

0
source

Source: https://habr.com/ru/post/1675442/


All Articles