简体   繁体   中英

UITableView Cell's expanded view doesn't show subviews?

I am expanding a UITableView cell on click.When the cell expands I have to load a UIView into it.My problen is that I am able to see the UIView on few occasions and sometimes it doesn't display ? The UIView is to be loaded in each and every expanded cell.

Expansion is done like this:-

 -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
  CGFloat kExpandedCellHeight =300;
  CGFloat normalCellHeight = 94;
  if ([self.expandedCells containsObject:indexPath]) {

    return kExpandedCellHeight;
  }else{
    return normalCellHeight;
}

}


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

static NSString *cellIdentifier = @"Cell";
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 = [[nameArray objectAtIndex:indexPath.row]valueForKey:@"opName"];


if (isExpanded) {
     backgroundView = [[UIView alloc]initWithFrame:CGRectMake(0, 95, 320,205)];
    [backgroundView setBackgroundColor:[UIColor colorWithRed:(235/255) green:(235/255) blue:(235/255) alpha:0.1]];
    [cell.contentView addSubview:backgroundView];

    container = [[UIView alloc]initWithFrame:CGRectMake(40, 67, 240, 120)];
    container.backgroundColor = [UIColor whiteColor];

    //I am adding buttons to this scrollview after webservice response,once buttons are loaded I am trying to load the above container on the background view.
    container_scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(20, 5, 220, 110)];
    [container_scrollView setBackgroundColor:[UIColor whiteColor]];
    [container addSubview:container_scrollView];      

  }

  return cell;

  }

Now I do get response from webservice.Buttons are added as well.However I can see the container view loaded sometimes and sometimes it doesn't show.What must be the reason?What is causing this behaviour?

This is how I load the container onto background view.

   //After container is loaded with buttons.
    if(backgroundView){
       [backgroundView addsubView:container];

     }

Declaration stuff:

  @interface ListViewController ()
 {

   UIView *backgroundView;//Used in expanded cell.
   UIView *container;
   BOOL isExpanded;  //I set this to NO in viewDidLoad initially.
   UIScrollView *container_scrollView;

}

You should consider having two different cells on your storyboard or two xib files, each with its correspoding cellidentifier. One will be your normal cell and the other the expanded cell. Then whenever a user taps on one normal cell you should 'replace'(redraw) that specific cell with your second type cell (an expanded cell).

UPDATE

For performing the update of the cells you could use any of these methods. more here

reloadData 
reloadRowsAtIndexPaths:withRowAnimation:
reloadSections:withRowAnimation:

Those will cause that some of your Table View Data Source delegate methods get called again, you should then use some logic to decide which kind of cell to draw. A quick draft:

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

    // Here you should implement your custom logic to check if you want a basic or expanded cell.
    if (IWantBasicCell) {
        cellIdentifier = @"basicCell";
    } else {
        cellIdentifier = @"expandedCell"
    }

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    [cell myCustomLoadInformationMethod:myCustomData];
    return cell;

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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