【问题标题】:How to add a subview to a tableview only covering the table cell but not the table header view如何将子视图添加到仅覆盖表格单元格但不覆盖表格标题视图的表格视图
【发布时间】:2011-09-01 15:03:45
【问题描述】:

我使用 UISegmentedControl 作为 tableview 的 headerview。现在我想添加加载视图(我自己定义的视图)只覆盖表格单元格而不是我的标题视图。我如何做到这一点?

【问题讨论】:

    标签: iphone xcode uitableview subview


    【解决方案1】:

    您可以将此视图添加到您想要的单元格中:

    UITableViewCell *cell = [self tableView:self.tableView cellForRowAtIndexPath:indexPathOfCell];
    [cell addSubview:view];
    

    【讨论】:

    • 你没有明白我的意思。如果像你说的那样做,每个单元格都会显示相同的加载视图,这也会影响原始显示。
    【解决方案2】:

    添加加载视图的最简单方法是这样的

    // get the frame of your table header view
    CGRect headerFrame = headerView.frame;
    
    // construct a frame that is the screen minus the space for your header
    CGRect loadingFrame = [[UIScreen mainScreen] applicationFrame];
    loadingFrame.origin.y += headerFrame.size.height;
    loadingFrame.size.height -= headerFrame.size.height;
    
    // use that frame to create your loading view
    MyLoadingView *loadingView = [[MyLoadingView alloc] initWithFrame:loadingFrame];
    
    // add your view to the window *
    [[headerView window] addSubview:loadingView];
    

    * 将视图添加到窗口可能不是您设计的最佳选择,但由于我不知道您的视图层次结构的任何细节,因此这种方式将始终有效。


    注意:如果您不隐藏分段控件,并且它已启用,则用户可能会在您不期望的情况下单击它并更改应用程序的状态 - 就像当您'正试图为他们加载一些东西。如果用户更改了应用程序的状态,请确保您可以取消此加载视图。

    【讨论】:

    • 我的视图是一个uitableview,顶部有一个导航栏。你能为我的视图层次修复它吗?
    • 这并不是我所说的详细程度 :) 我很确定如果您尝试用自己的一些观点代替 [headerView window],您将能够很快就弄清楚了。