繁体   English   中英

UITableViewCell内的UITableView子视图不会显示

[英]UITableView subview inside UITableViewCell won't display

我正在尝试创建一个UITableview,其中每个单元格包含另一个UITableView。 它基本上是一个2-3行的列表,然后每一行都有一个2-3行的子列表。

主ViewController的行为与其他TableViewController一样,在cellForRowAtIndexPath内部,它尝试添加具有动态高度的SubTableViewController.view。 SubTableViewController是第二个表视图,每个都有自己的数据源。

我遇到的问题是SubTableViewController似乎可以使用调试器正确渲染,并具有正确的数据和单元格数量,但是在渲染时根本没有出现。 我注意到它的ViewDidAppear中的contentSize高度为“ 0”,尽管生成了2+行带有高度的行。 该项目使用AutoLayout,但是在添加时以编程方式设置了SubTableViewController的框架。

有什么想法可以使我出现在这里吗?

数据结构(期间为子表)

[{name:"activity 1",
  periods:[{name:"period1",desc:"desc1"},
           {name:"period2",desc:"desc2"},
           {name:"period3",desc:"desc3"}
          ]
 },
 {name:"activity 2",
  periods:[{name:"period1",desc:"desc1"},
           {name:"period2",desc:"desc2"},
           {name:"period3",desc:"desc3"}
          ]
 }]

ViewController

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [_data count];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];

    UIImageView *imageView = (UIImageView*)[cell viewWithTag:1];
    UIView *subTableView = (UIView*)[cell viewWithTag:3];

    return imageView.frame.size.height + subTableView.frame.size.height + 20;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *MyIdentifier = @"tableViewCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    if (cell == nil){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    ActivityModel *model = _data[indexPath.row];

    UILabel *nameLabel = (UILabel*)[cell viewWithTag:2];
    nameLabel.text = model.name;

    UIView *internalView = (UIView*)[cell viewWithTag:10];

    SubTableViewController *subTableViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"SubTableViewController"];
    subTableViewController.activityModel = model;
    subTableViewController.view.tag = 3;
    subTableViewController.view.frame = CGRectMake(0,
                                                   60,
                                         internalView.frame.size.width,                                        
                       subTableViewController.view.frame.size.height);
    [internalView addSubview:subTableViewController.view];
    [internalView bringSubviewToFront:subTableViewController.view];
    [subTableViewController.tableView reloadData];


    return cell;
}

SubTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];


    int height = 0;
    for (int section = 0; section < [self numberOfSectionsInTableView:_tableView]; section++){
        for(int row = 0; row < [self tableView:_tableView numberOfRowsInSection:section]; row++){
            height += [self tableView:_tableView heightForRowAtIndexPath:[NSIndexPath indexPathForItem:row inSection:section]];
        }
    }

    CGRect tableFrame = self.tableView.frame;
    tableFrame.size.height = height;
    self.tableView.frame = tableFrame;

    CGRect viewFrame = self.view.frame;
    viewFrame.size.height = height;
    self.view.frame = viewFrame;
    [self.view bringSubviewToFront:_tableView];

    // [self.tableView layoutIfNeeded];
    // [self.tableView setNeedsDisplay];
    // [self.tableView reloadData];
    // _tableView.contentSize = CGSizeMake(600, 52);

    self.view.backgroundColor = [UIColor redColor];
    self.tableView.backgroundColor = [UIColor blueColor];

    self.tableView.hidden = NO;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [_activityModel.periods count];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
    UILabel *nameLabel = (UILabel*)[cell viewWithTag:3];
    UITextView *textView = (UITextView*)[cell viewWithTag:5];

    float textViewHeight = [textView.text sizeWithFont:[UIFont systemFontOfSize:13] constrainedToSize:CGSizeMake([UIScreen mainScreen].bounds.size.width - 40, MAXFLOAT) lineBreakMode:NSLineBreakByWordWrapping].height;

    return nameLabel.frame.size.height + textViewHeight + 20;

}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"tableViewCell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    PeriodModel *model = _activityModel.periods[indexPath.row];

    UILabel *nameLabel = (UILabel*)[cell viewWithTag:3];
    UILabel *durationLabel = (UILabel*)[cell viewWithTag:4];
    UITextView *textView = (UITextView*)[cell viewWithTag:5];

    nameLabel.text = model.name;
    durationLabel.text = [NSString stringWithFormat:@"%d minutes", (int)(model.durationSeconds/60)];

    textView.text = model.description;

    textView.textContainerInset = UIEdgeInsetsZero;
    textView.textContainer.lineFragmentPadding = 0;
    textView.scrollEnabled = NO;

    CGRect frame = textView.frame;
    frame.size.height = [textView.text sizeWithFont:[UIFont systemFontOfSize:12] constrainedToSize:CGSizeMake(cell.contentView.frame.size.width, MAXFLOAT) lineBreakMode:NSLineBreakByWordWrapping].height;
    textView.frame = frame;

    return cell;
}

模拟器图片。 标题是外部的UITableCell,其下方是红色的SubTableViewController.view。 UITableView应该覆盖红色,但此处根本不出现。 它不是隐藏的,在下面等。

在此处输入图片说明

不要将表格视图放在单元格中。

对于您描述的用途执行此操作几乎没有好处。 使用节和行。 每个部分都是您当前的单元格。 并且该部分中的每一行都是您当前嵌入式表视图中的一行。

此外,如果内部表格视图的大小不同,则需要计算每个外部单元格的所有内部单元格高度。 这正是节和行所发生的事情,但是作为开发人员,您只需要担心行, UITableView将处理节的​​大小。

暂无
暂无

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

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