UITableView reloadData无法与自定义子视图一起正常工作

时间:2014-03-22 19:47:42

标签: ios iphone objective-c uitableview

所以我有一个非常适合添加到视图控制器的视图,其内容基于一个包含从UIView继承的对象的数组:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"OptionCell" ];
  if (cell == nil)
  {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"OptionCell"];
  }
  while ([cell.contentView.subviews lastObject] != nil) {
    [(UIView*)[cell.contentView.subviews lastObject] removeFromSuperview];
  }
  OPTChainRowView *chainRowView = (OPTChainRowView *)[_chainRowViews objectAtIndex:indexPath.row];
  [cell.contentView addSubview:chainRowView];
  return cell;
} 

用户可以选择"拉动刷新"通过UIRefreshControl对象通过函数调用刷新表,它基本上在后台提取数据然后更新主线程:

-(void) refreshTable
{
  dispatch_queue_t backgroundQueue = dispatch_queue_create("com.OPT.chainTableQueue", 0);
  dispatch_async(backgroundQueue, ^{
    [self updateOptionsChain];
    dispatch_sync(dispatch_get_main_queue(), ^{
      [self performSelector:@selector(stopRefresh) withObject:nil afterDelay:1.0];
    });
  });
}

这里有updateOptionsChain (我在下面解释一下backgroundRowViews数组)

- (void)updateOptionsChain
{
  if (_backgroundRowViews == nil)
    _backgroundRowViews = [[NSMutableArray alloc] init];
  [_backgroundRowViews removeAllObjects];
  int index = 0;
  NSMutableArray *optionsChain = [_delegate getOptionsChain];
  for (id object in optionsChain)
  {
    OPTChainRow *chainRow = (OPTChainRow*)object;
    OPTChainRowView *chainRowView = [[OPTChainRowView alloc] initWithFrame:CGRectMake(0.0, 0.0, ROW_WIDTH, ROW_HEIGHT)];
    [chainRowView setUpRow:chainRow];
    [chainRowView setParent:self];
    [chainRowView setIndexInChain:index];
    [_backgroundRowViews addObject:chainRowView];
    index++;
  }
}

stopRefresh只是

-(void)stopRefresh
{
  [_refreshControl endRefreshing];
  _chainRowViews = [NSMutableArray arrayWithArray:_backgroundRowViews];
  [_chainTableView reloadData];
}

用于更新表视图的数组是_chainRowViews(参见上文)。 我有一个" backgroundRowView" array,这是在后台更新的数组。 我执行此操作,因此在查看表格时我不会更新表格的视图数据;我只是"同步"或者在刷新后更新表视图的视图数组(_chainTableView)。

该表似乎重新加载,但实际的行不可见(即不要更新),直到我点击它们。通常一段时间后(比如几秒钟之后)整个表格都会正常刷新。如果我再次刷新表格,则所有旧行都会消失,并且直到点击/延迟刷新行为才会显示""再次发生。

如果我要在cellForRowAtIndexPath中的每个单元格中手动添加一些随机UILabel,而不是我的自定义视图类,该表工作正常...不确定为什么我的自定义视图单元格没有。请记住,这些表格相当小,比如70-80行,所以我认为它们会很快刷新。

0 个答案:

没有答案