1

I am working in a project which is in Objective C and Xcode version is 10.1. In this project I am using socket. In this app I am continuously reading data from Socket. And I am showing the updated value into table view. For that I am reloading the table section header as fast as data comes from socket. Means If I get id = 5 then I try to find index of section whose id == 5 and I will update that section only.

Now the problem is when I tap on table header it opens inner row of that section header and after that when I scroll down, the table-view moving up and down like jumping continuously because data is coming in such a fast way.

For Ex. : Table View

Case 1 :(Scroll is working perfectly)

  • Section Header 1 (When I click on Header 1, Row 1 will appear)
  • Section Header 2
  • Section Header 3
  • Section Header 4
  • Section Header 5

Case 2 : (Scroll will lead to Jumping Headers)

  • Section Header 1 (When I click on Header 1, Row 1 will be removed)
    • Row 1
  • Section Header 2
  • Section Header 3
  • Section Header 4
  • Section Header 5

So in above structure, when we click on Header 1 and then we scroll down, at that time next headers are jumping because of continuous update process.

This jumping header effect in scroll, is not coming in Case 1 scenario. This happened only in Case 2.

This problem was not occurring when I was reloading the whole table instead of reloading the only one section header at a time. Please help me in this issue. Thanking you in advance.

I tried to reload section header like mentioned in below. [tableView beginUpdates]; [tableView reloadSections:[NSIndexSet indexSetWithIndex:i] withRowAnimation:UITableViewRowAnimationNone]; [tableView endUpdates];

3 Answers3

1

If you are using "heightForRowAtIndexPath:" method to calculate the height. Use "estimatedHeightForRowAtIndexPath" this method also. Write code in row height method.

phani
  • 72
  • 6
0

When you reload the section of the UITableView, it will try to do the whole render process again. That means, it will get the number of rows, number of sections, get the appropriate cell for that row or section, will calculate height of each one of them and then draw on screen.

This whole process takes a lot of time. Since all the UI operations happen on Main thread which is a serial thread, and you are updating the table view section continuously and very fast, this will cause a lot of jitter.

A better way to handle such a scenario is to delay the updates a little. Say in an interval of 1 min, take the latest update. This can be achieved using RxSwift or may be implement your own logic.

Then after you do that, an optimization that you can attempt is to update the section view yourself rather than letting UITableView doing it for you. This means, that based on the current state of the UITableView, find the section you want to update with the data, use indexPathsForVisibleRows and cellForRow(at indexPath: IndexPath) to get the cell and directly update the view.

This method will work fine if the new data does not change the height of the cell or section. Otherwise, you will have to invalidate the height of the cell which will introduce a little bit of jitter.

kerry
  • 2,362
  • 20
  • 33
  • Thnks for reply @kerry. In my case i am displaying only section header. When user tap on section header at any index then cell will be visible for that index only. I tried to find the way to get visible section header, but in all solution i found that visible section get from visible row. [https://stackoverflow.com/questions/4191317/how-to-get-a-uitableviews-visible-sections] – Jaykar Parmar Jun 19 '19 at 07:02
  • and you are updating the row? – kerry Jun 19 '19 at 07:06
  • Yes i am updating the row as well. – Jaykar Parmar Jun 20 '19 at 04:52
  • @JaykarParmar it should not be a problem. You can just get the section view as well as the row view and update them in place. – kerry Jun 20 '19 at 05:46
0

If you reload section, then everything to do with that section will be redrawn, including row numbers, header contents, and default section behaviour (closed or expanded). you need to change your design or your approach to the solution required - for example, make the table non-closable (ie header and childs are always visiblle), or other things.

GeneCode
  • 7,545
  • 8
  • 50
  • 85