cell添加tableview

在仿淘宝详情页时,有个需求,顶部有导航栏

每个标签下对应一个tableview,现在想把第2页的内容添加到第1页下,直接添加时出现了问题。

if ([cellType isEqualToString:@"SHProDetail_Cell"]){
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellType];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:cellType];
    }
    self.detailView.tableView.scrollEnabled = NO;
    [cell.contentView addSubview:self.detailView.tableView];
    return cell;
}

这样添加会出现该cell内容不能完整展示的问题,虽然cell高度是正确的,数据也没问题,但就是无法完全展示。
可以换一种添加方式,将第2页内容添加到UIView上,而不是tableview

if ([cellType isEqualToString:@"SHProDetail_Cell"]){
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellType];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:cellType];
    }
    self.detailView.tableView.scrollEnabled = NO;
    UIView *tempView = (UIView *)self.detailView.tableView;
    //_detailPageHeight为第2页tableview的contentSize高度,还有header、footer高度之和
    tempView.frame = CGRectMake(0, 0, kScreenWidth, _detailPageHeight);
    [cell.contentView addSubview:tempView];
    return cell;
}

这样即可完美解决。

原文地址:https://www.cnblogs.com/Apologize/p/6904887.html