0

I am facing the issue of not calling the delegate method when I am trying to reload table view from the tableViewDidSelect method.

if indexPath.row == 0 {
                isTermAndConditionExpanded = !isTermAndConditionExpanded
                tableView.reloadData()
                if isTermAndConditionExpanded {
                    tableView.scrollToRow(at: IndexPath(row: 1, section: indexPath.section), at: .bottom, animated: true)
                } else {
                    tableView.scrollToRow(at: IndexPath(row: 0, section: indexPath.section), at: .bottom, animated: true)
                }

based on isTermAndConditionExpanded I am changing the numberOfRowsinsection. since it is not calling its getting crash. I checked before reloading table view delegate and data source is properly set to present the view controller

  • Add the crash report too. – Ankit Jayaswal Apr 13 '18 at 10:25
  • *** Terminating app due to uncaught exception 'NSRangeException', reason: '-[UITableView _contentOffsetForScrollingToRowAtIndexPath:atScrollPosition:usingPresentationValues:]: row (1) beyond bounds (1) for section (5). – Abhishek Kashyap Apr 13 '18 at 10:31
  • 1
    `reloadData()` is not guaranteed to be synchronous - see this answer https://stackoverflow.com/questions/9675667/is-uitableview-reloaddata-asynchronous-or-synchronous - if it's not, all you've done is invalidated the content - you need to do the scrolling once your'e sure the data has been loaded again... – Grimxn Apr 13 '18 at 10:33
  • I tried tableView.reloadData inside DispatchQueue.main.async block of code. tried tableview.setLayoutIfneededas well. nothing worked – Abhishek Kashyap Apr 13 '18 at 10:36
  • comment your `scrollToRow` code and reload will work. From the exception, it is clear that this line is causing the crash. – Kamran Apr 13 '18 at 10:51
  • @kamran issue is not crash, issue tableView.reload is calling delegate method. once delegate method gets called, it won't cause the crash. – Abhishek Kashyap Apr 13 '18 at 11:12
  • @AbhishekKashyap The issue seems to be of you data, Like for example you want to reload the table to reflect the changes in your data, But i guess in your case the table is reloaded before the data is reflected and it is not passed to the table – Abhirajsinh Thakore Apr 14 '18 at 05:44
  • @AbhirajsinhThakore reload is not happening after I try to resolve to resign first responder issue, this worked for me but causing this issue. you can check the solution. [link](https://stackoverflow.com/a/49726515/9522367) – Abhishek Kashyap Apr 14 '18 at 14:34

0 Answers0