当 UITableView 完全重新加载时 [英] When UITableView is fully reloaded

查看:26
本文介绍了当 UITableView 完全重新加载时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个控件可以部分或完全更改 tableView 的内容.更改发生后,我设置了一个标志 tableViewContentHasChanged:

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;

我的问题出现在tableView:viewForHeaderInSection:;在重新加载表视图之后调用它,因此我的标志在该方法中无效.

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

简而言之:当表完全重新加载时,观察表的正确方法是什么,以便我可以将标志设置为NO?而且,我可能做错了什么?

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?

推荐答案

我认为处理这个问题的最好方法是在其他人提到的数据模型中,但如果你真的需要这样做,你可以执行以下操作:

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:

>

根据Apple 的文档,当您调用 reloadData

所以你需要知道最后一个可见的标题是什么时候呈现的,所以你设置:

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

  tableViewContentHasChanged = YES;
  [self.tableView reloadData];

然后在 cellForRowAtIndexPath 中:获取最后显示的索引并将其存储在成员变量中:

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;
}

这样当 viewForHeaderInSection: 被调用时,你就会知道哪个是该重新加载事件中的最后一个标题:

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;
}

请注意,此方法仅在最后一个可见部分至少有 1 行时才有效.

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

希望这会有所帮助.

这篇关于当 UITableView 完全重新加载时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆