0

I have two custom cells, but having trouble setting static heights to both those cells, I need the first cell to have a height of 100, but every other cell to have a height of 40. The below code makes all the cells have a height of a 100 instead of just the first one.

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if(indexPath.row == 0) {
            return 100.0
        }
            return 40.0
    }
mfaani
  • 33,269
  • 19
  • 164
  • 293
joethemow
  • 1,641
  • 4
  • 24
  • 39
  • That post does not mention anything about heights. – joethemow Apr 19 '17 at 17:44
  • 1
    Joe see the second linked duplicated: http://stackoverflow.com/questions/39071603/swift-how-to-set-dynamic-cell-height-in-tableviewcontroller-which-contains-more – JAL Apr 19 '17 at 17:45

1 Answers1

0

You can put your first cell in a different section.

func numberOfSections(in tableView: UITableView) -> Int {
    return 2
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if section == 0 {
        return 1
     }else {
        return yourArray.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if indexPath.section == 1 {
        let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! FirstCustomCell
        return cell
    }else{
        let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! SecondCustomCell
        return cell
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    if indexPath.section == 1 {
        return 100
    } else{
        return 40
}
Mat
  • 6,236
  • 9
  • 42
  • 55