繁体   English   中英

展开单元格时,UITableView单元格未在某些单元格中显示数据

[英]UITableView cells not showing data in some cells when a cell is expanded

我在tableView didSelectRowAtIndexPath期间加载了一个扩展的单元格XIB 。现在每个单元格扩展时我都调用一个Web服务,根据它的响应,我在扩展的单元格中加载了childViewController 。现在的问题是数据在某些单元格中可见它不在其他单元中吗? 不完全确定dequeueReusableCellWithIdentifier重复使用单元格是否会导致此类问题。但是,如果那样,我该如何解决该问题?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

static NSString *cellIdentifier = @"Cell";
static NSString *expandedCellIdentifier = @"ExpandedCell";
if (!isExpanded) {

  ListCell *cell =(ListCell*) [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell==nil) {
        NSArray *nibs = [[NSBundle mainBundle]loadNibNamed:@"ListCell" owner:self options:nil];
        cell = nibs[0];

    }
 cell.Name.text = [[bArray objectAtIndex:indexPath.row]valueForKey:@"opName"];
 return cell;

  }

 else{

 expCell = (ExpandedCell*)[tableView dequeueReusableCellWithIdentifier:expandedCellIdentifier];
 if (expCell==nil) {
        NSArray *nibs = [[NSBundle mainBundle]loadNibNamed:@"ExpandedCell" owner:self options:nil];
        expCell = nibs[0];

     UILabel *end = [[UILabel alloc]initWithFrame:CGRectMake(90, 24, 70, 14)];

    [end setTag:102];

    [expCell.background_View addSubview:end];

  }
    UILabel *endLabel = (UILabel *)[expCell.background_View viewWithTag:102];
    endLabel.text = [NSString stringWithFormat:@"%@",endStn.capitalizedString];

    return expCell;

}

return nil;

}


 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

if ([self.expandedCells containsObject:indexPath]) {

    [self.expandedCells removeAllObjects];
 }else{
    isExpanded=YES;

    if (self.expandedCells.count>0) {
        [self.expandedCells removeAllObjects];
    }

    [self.expandedCells addObject:indexPath];

    //Call webservice and populate the view controller to be loaded.
     [self callWebservice completionBlock:^(BOOL finished){

        if (finished) {


            [self initialseChildViewController:^(BOOL finished){
                if (finished) {

                    [self populateView:^(BOOL finished){

                        if (finished) {

                            if (expCell.expContainer.hidden==YES) {
                                expCell.expContainer.hidden=NO;
                            }


                        }else{
                            NSLog(@"Data not populated");
                        }

                    }];


                }else{
                    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"" message:@"ChildViewController not initialised" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
                    [alert show];
                }

            }];


        }

    }];
    [self.busTableView beginUpdates];
    [self.busTableView reloadData];
    [self.busTableView endUpdates];

}

如下修改您的代码。 您需要首先在if条件中返回.cell。在if(!isExpanded)条件下返回nil。 修复如下

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

static NSString *cellIdentifier = @"Cell";
static NSString *expandedCellIdentifier = @"ExpandedCell";
if (!isExpanded) {

  ListCell *cell =(ListCell*) [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell==nil) {
        NSArray *nibs = [[NSBundle mainBundle]loadNibNamed:@"ListCell" owner:self options:nil];
        cell = nibs[0];

    }
 cell.Name.text = [[bArray objectAtIndex:indexPath.row]valueForKey:@"opName"];

return cell;

  }

 else
{

 expCell = (ExpandedCell*)[tableView dequeueReusableCellWithIdentifier:expandedCellIdentifier];
 if (expCell==nil) 
  {
        NSArray *nibs = [[NSBundle mainBundle]loadNibNamed:@"ExpandedCell" owner:self options:nil];
        expCell = nibs[0];
  } 

    UILabel *endLabel = (UILabel *)[expCell.background_View viewWithTag:102];
    endLabel.text = [NSString stringWithFormat:@"%@",endStn.capitalizedString];

    return expCell;

}

return nil;

}

希望对您有帮助。

您对didSelectRow中的单元格所做的任何事情都是可疑的,尤其是您异步进行的事情。 当用户滚动时,这些单元格会进出。 当单元在另一个indexPath中重用时,直接对单元进行的状态更改将显示在其他位置。

相反,您应该遵循的模式是:(1)用户采取行动,(2)行动改变模型,(3)模型改变导致表更新,(4)表更新读取模型。

在这种情况下,这意味着此代码...

 [self callWebservice completionBlock:^(BOOL finished){
     if (finished) {
         [self initialseChildViewController:^(BOOL finished){
             if (finished) {
                 [self populateView:^(BOOL finished){

...不应引用任何表单元格(无论是在方法中还是在完成块中)。 您发布的唯一修改单元格的代码在以下几行中:

                     if (expCell.expContainer.hidden==YES) {
                         expCell.expContainer.hidden=NO;
                     }

...甚至那些必须改变。 无论您对单元格做什么,都必须在cellForRowAtIndexPath中完成。 在模型中记下该indexPath处的单元格expContainer的隐藏状态必须更改(不确定在您的项目中是否等同于添加到expandedCells集合中),然后在此indexPath处重新加载该行。

重新声明规则:仅更改cellForRowAtIndexPath中的单元格。 根据模型(即数据源数组)更改这些单元格。 如果模型中的信息不足以告诉您如何配置单元,则您的模型缺少某些内容……将其添加到模型中。 然后,当您想对表格单元做任​​何事情时,就不要这样做。 对您的模型做些事情,然后重新加载表的那部分。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM