简体   繁体   English

UITableView Cell的展开视图不显示子视图?

[英]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 ? 我在单击时扩展了一个UITableView单元格。当该单元格扩展时,我必须将一个UIView加载到其中。我的问题是我能够在少数情况下看到UIView,有时它不显示? The UIView is to be loaded in each and every expanded cell. UIView将被加载到每个扩展的单元格中。

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? 现在我确实从webservice得到响应,同时也添加了按钮,但是我有时看到加载的容器视图有时不显示,这必须是什么原因,是什么原因引起的?

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. 您应该考虑在情节提要上有两个不同的单元格或两个xib文件,每个单元格都有其对应的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;

} }

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

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