1

我制作了一个带有原型单元格的 TableView;在 tableView 的第一个位置我做了这个

override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    if section == 0
    {
        let view : UIView = UIView(frame: CGRectMake(0, 0, 400, 140))
        view.backgroundColor = UIColor.redColor()
        view.addSubview(populateView())
        //populateView() is a method by which I programmatically made all object in the view (label, images, textView)
        return view
    }
    else
    {
        return nil
    }
}

override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat
{
    if section == 0
    {
        return 140
    } else
    {
        return 0
    }
}

结果是这样的(这是一个测试): 截屏

现在我想让 TableHeaderView 不与 tableView 一起滚动;我将 tableView 样式设置为简单;我发现了 UITableView Section Header 的 Change Default Scrolling Behaviorhttps://corecocoa.wordpress.com/2011/09/17/how-to-disable-floating-header-in-uitableview/但我不知道Objective-c(我是 6 个月的快速程序员)。谁能解决我的问题?

4

2 回答 2

3

解决方案是使用 a UIViewController,而不是 a UITableViewController。表格视图控制器将占据整个view空间,并且上面没有空间。

相反,您必须使用 a UIViewController,添加一个子视图作为标题,并将 aUITableView也添加为子视图。您可以通过显式包含数据源和委托协议来填充它并与之交互。

class HeaderTableViewController : UIViewController, 
                                  UITableViewDataSource, UITableViewDelegate
于 2015-08-01T19:00:54.197 回答
3

我肯定会使用 UIViewController 和 UITableView 和它上面的自定义视图(见下文)。

在此处输入图像描述

或者如果你想使用 UITableViewController(它带来了一些额外的功能),你可以使用容器视图将它嵌入到标题下。

在此处输入图像描述

于 2015-08-01T20:13:12.437 回答