ios 使用故事板时如何在 IB 中为 UITableView 添加页脚视图?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19911536/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 02:51:16  来源:igfitidea点击:

How to add footer view for UITableView in IB when working with storyboards?

iosobjective-cuitableviewstoryboardautolayout

提问by Centurion

Playing around with prototype cells and I need to add footer view at the bottom of table view, so a user could see this footer view when he would scroll to the bottom of the table view. So, created demo project with one screen, table view and two prototype cells. Looking for a way how to drag and drop some view below the table using Interface Builder. The problem is it looks like view is put outside table view content, so I see the footer but I can't scroll to it (only a small part of footer view is seen at the bottom of the table view).

玩弄原型单元格,我需要在表格视图的底部添加页脚视图,以便用户在滚动到表格视图底部时可以看到这个页脚视图。因此,创建了具有一个屏幕、表格视图和两个原型单元格的演示项目。寻找如何使用 Interface Builder 在表格下方拖放某些视图的方法。问题是视图看起来像是放在表格视图内容之外,所以我看到了页脚,但我无法滚动到它(在表格视图的底部只能看到页脚视图的一小部分)。

I know this should work because already saw a working implementation but cannot figure out what magic setting or code line I need to add.

我知道这应该有效,因为已经看到了一个有效的实现,但无法弄清楚我需要添加什么魔法设​​置或代码行。

Here are the methods:

以下是方法:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

  return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section {

  return 2;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath  *)indexPath {

  CGFloat rowHeight = 10;

  switch (indexPath.row) {

    case 0: {

        rowHeight = 376;

        break;
    }
    case 1: {

        rowHeight = 105;

        break;
    }
  } 

  return rowHeight;
}

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

  switch (indexPath.row) {

    case 0: {

        Cell1 *cell1 = [tableView dequeueReusableCellWithIdentifier:@"Cell1" forIndexPath:indexPath];
        return cell1;

        break;
    }
    case 1: {

        Cell2 *cell2 = [tableView dequeueReusableCellWithIdentifier:@"Cell2" forIndexPath:indexPath];
        return cell2;

        break;
    }
  }

  return nil;
}

Just making 2 row table with two different height cells. Cell1 and Cell2 classes are empty subclasses of UITableViewCell.

只需制作带有两个不同高度单元格的 2 行表格。Cell1 和 Cell2 类是 UITableViewCell 的空子类。

Here's how table view and cells look in interface builder:

以下是界面构建器中表格视图和单元格的外观:

enter image description here

在此处输入图片说明

Here's initial view after launch:

这是启动后的初始视图:

enter image description here

在此处输入图片说明

Here's what I see if I scroll to the bottom:

如果我滚动到底部,我会看到以下内容:

enter image description here

在此处输入图片说明

The footer is there, but outside table view (scroll content). As you can see, table view by default reserves some 44px space at the bottom for footer. But if I set footer height in tableView:heightForFooterInSection: then blank spaces appear.

页脚在那里,但在表格视图之外(滚动内容)。如您所见,表格视图默认在底部保留一些 44 像素的空间作为页脚。但是,如果我在 tableView:heightForFooterInSection: 中设置页脚高度,则会出现空格。

Also, tried to drag and move this view up to view hierarchy in IB, so the view would become a header view. In that case, the view is shown at the top as header view, but then the second cell is shown only partially when scrolling to the bottom. It looks like table calculates how much space it needs to show prototype dynamic cells (have set "Dynamic Prototypes" for the table view). And if you add extra footer or header view to the interface builder then there's less space for the cells (if view is added as header) or the footer is not shown.

此外,尝试将此视图向上拖动并移动到 IB 中的视图层次结构,因此该视图将成为标题视图。在这种情况下,视图在顶部显示为标题视图,但第二个单元格在滚动到底部时仅部分显示。看起来 table 会计算显示原型动态单元格所需的空间(已为 table view 设置了“Dynamic Prototypes”)。如果您向界面构建器添加额外的页脚或页眉视图,那么单元格的空间就会减少(如果视图添加为页眉)或页脚不会显示。

UPDATE. If I add this method then blank spaces appear:

更新。如果我添加此方法,则会出现空格:

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {

  return 50;
}

Here's what I get in that case (blank spaces below cell2):

这是我在这种情况下得到的(单元格 2 下方的空格):

enter image description here

在此处输入图片说明

UPDATE2Footer is shown correctly if I disable autolayout.

如果禁用自动布局,UPDATE2页脚将正确显示。

回答by iphonic

I think you have missed the following line

我认为您错过了以下行

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{

    return HEIGHT_OF_YOUR_FOOTER_VIEW;

}

Edit

编辑

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{

    UIView *footer=[[UIView alloc] initWithFrame:CGRectMake(0,0,320.0,50.0)];
    footer.layer.backgroundColor = [UIColor orangeColor].CGColor;


    return footer;    
}

Updated

更新

Check this documentation

检查此文档

回答by Centurion

At last found the solution. Have noticed footer is shown correctly when autolayout is disabled, so started to look at constraints. So, have added leading space to container, trailing space, bottom space and top space to container constraints to table view using Ctrl + Drag. However IB showed red warning about missing Y position constraint. So, after choosing "Add missing constraints" from IB suggestion panel, IB added another system constraint but the footer was still not show correctly. It appears IB was unable to add correct top space and bottom space to container system constraints, and even to fix that. So I'v got 5 system constraints as a result of that:

终于找到了解决办法。注意到禁用自动布局时页脚显示正确,因此开始查看约束。因此,使用 Ctrl + 拖动将前导空间添加到容器、尾随空间、底部空间和顶部空间到容器约束到表视图。然而,IB 显示了关于缺少 Y 位置约束的红色警告。因此,在从 IB 建议面板中选择“添加缺少的约束”后,IB 添加了另一个系统约束,但页脚仍未正确显示。看起来 IB 无法为容器系统约束添加正确的顶部空间和底部空间,甚至无法修复它。因此,我得到了 5 个系统约束:

enter image description here

在此处输入图片说明

Adding system constraints using Ctrl + Drag from table view to containing view have worked in previous demos for me. However, this time IB was unable to add correct top space and bottom space to container, and so top space vertical constraint had a value of -568. Tried setting to 0 but it didn't worked. Tried ten times to delete all constraints and add them again. The same result.

使用 Ctrl + 从表视图拖动到包含视图添加系统约束在之前的演示中对我有用。但是,这次IB无法为容器添加正确的顶部空间和底部空间,因此顶部空间垂直约束的值为-568。尝试设置为 0 但它没有用。尝试了十次删除所有约束并再次添加它们。同样的结果。

So, I deleted all these vertical (bottom and space) constraints and then selected "Add missing constraints". And bingo! IB added correct vertical constraints and the footer view was shown correctly. Here's how correct constraints should look like. However, I still don't understand why IB was unable to add correct constraints when I was doing Ctrl + Drag from table view to container view.

因此,我删除了所有这些垂直(底部和空间)约束,然后选择了“添加缺少的约束”。还有宾果游戏!IB 添加了正确的垂直约束并且页脚视图显示正确。以下是正确约束的样子。但是,我仍然不明白为什么当我在从表视图到容器视图执行 Ctrl + Drag 时,IB 无法添加正确的约束。

enter image description here

在此处输入图片说明