簡體   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