user1244109
user1244109

Reputation: 2186

When UITableView is fully reloaded

I have a control that partially or fully changes content of tableView. After the change occurred, I set a flag tableViewContentHasChanged:

BOOL tableViewContentHasChanged = YES;
[self.tableView reloadData];
tableViewContentHasChanged = NO;

My problem appears in tableView:viewForHeaderInSection:; it is called after the table view is reloaded, so my flag is not effective inside that method.

In short: what's the right way to observe when the table has fully reloaded, so I could set the flag to NO? And, what am i possibly doing wrong?

Upvotes: 0

Views: 551

Answers (1)

Mariam K.
Mariam K.

Reputation: 620

I think the best way to handle this is in the data model as others mentioned but if you really need to do this, you can do the following:

According to Apple's documentation, only visible sections/cells are reloaded when you call reloadData

so you need to know when the last visible header is rendered so you set:

  tableViewContentHasChanged = YES;
  [self.tableView reloadData];

Then in cellForRowAtIndexPath: get the last displayed index and store it in a member variable:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  //Your cell creating code here
  UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"TryCell"];

  //Set last displayed index here
  lastLoadedSectionIndex = indexPath.section;
  NSLog(@"Loaded cell at %@",indexPath);
  return cell;
}

That way when viewForHeaderInSection: is called you'll know which is the last header in that reload event:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
  //Create or customize your view
  UIView *headerView = [UIView new];

  //Toggle tableViewContentHasChanged when it's the last index
  if (tableViewContentHasChanged && section == lastLoadedSectionIndex) {
    tableViewContentHasChanged = NO;
    NSLog(@"Reload Ended");

  }
  return headerView;
}

Please note that this method will only work if last visible section has at least 1 row.

Hope this helps.

Upvotes: 2

Related Questions