1

I have a table view with cells having dynamic height. I want to add a new row on a button click. I am incrementing the number of rows in section value and reloading the table view.But this results in a crash.I tried this after commenting the following lines

func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
    return 200
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {



    return UITableViewAutomaticDimension

}

This is working fine when these 2 delegate methods are commented.But I want to add a new row.Dynamic height cells should be possible .How can I achieve this?

  • what error you are getting? there may be many many reason, may you are trying to get deallocated variable – Rajeev Singh Aug 09 '17 at 05:29
  • I am receiving (lldb) error. #0 0x0000000113d329dc in -[UISectionRowData refreshWithSection:tableView:tableViewRowData:] () –  Aug 09 '17 at 05:39
  • Please check this link https://stackoverflow.com/questions/15522554/why-assertion-failure-in-uisectionrowdata-refreshwithsectiontableviewtablevi – Rajeev Singh Aug 09 '17 at 05:46

1 Answers1

1

You can do like this

 numberOfItems += 1
    let indexPath = IndexPath(row: self.numberOfItems - 1, section: 0)
                self.tbl.beginUpdates()
                self.tbl.insertRows(at: [indexPath], with: .automatic)
                self.tbl.endUpdates()
Jaydeep Vyas
  • 4,411
  • 1
  • 18
  • 44
  • Hi I tried this method but I am getting (lldb) at self.tableView.endUpdates() –  Aug 09 '17 at 05:48
  • make sure before calling this your datasource array should be updated and also increment number of items += 1 for adding row at last index – Jaydeep Vyas Aug 09 '17 at 05:50