xcode 我如何保持标题单元格与 Swift 2.0 中的 tableview 单元格一起移动

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35738415/
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-09-15 08:35:56  来源:igfitidea点击:

How do i keep the header cell moving with the tableview cells in Swift 2.0

iosxcodeswiftuitableview

提问by BlueIceSky

I am trying to create a tableview which starts the header in the middle of the tableview and then can be scrolled up with the added tableView Cells to the top then stops, and then the tableview cells can just scroll "under" it. I got the header to be in the middle of the tableview, but the tableView Cells just scroll on top of it, and the header doesnt really scroll to the top. it just stay where it is. I want to be able to let the header move as you scroll the list, and then stops when it reaches the top of the list, and then its just the tableView Cells that are moving afterwards.

我正在尝试创建一个 tableview,它在 tableview 的中间启动标题,然后可以使用添加的 tableView Cells 向上滚动到顶部然后停止,然后 tableview 单元格可以在它的“下方”滚动。我让标题位于 tableview 的中间,但 tableView Cells 只是在它上面滚动,标题并没有真正滚动到顶部。它只是留在原地。我希望能够在滚动列表时让标题移动,然后在它到达列表顶部时停止,然后它只是之后移动的 tableView 单元格。

I am using the latest version of XCode

我正在使用最新版本的 XCode

回答by Devang Tandel

GO TO STORYBOARD > SELECT VIEW > SELECT TABLEVIEW > PROPERTY INSPECTOR > CHNAGE STYLE TO GROUPPED from PLAIN

转到故事板 > 选择视图 > 选择表格视图 > 属性检查器 > 将样式更改为从 PLAIN 分组

Don't forget to handle footerview. In Groupped mode a footer will appear and arrange background or other properties. They will change.

不要忘记处理页脚。在分组模式下,将出现一个页脚并排列背景或其他属性。他们会改变。

see Image : enter image description here

见图片: 在此处输入图片说明

回答by Bharat Modi

You can do it using sections. If i understood your question correctly, what you want to do is, you have to display header in the middle of screen while having some cells above the header and some cells below the header, and when you scroll the tableView up your header should be scrolled to top and when it reaches to the top you want your header to keep at the top while cells should be scrolled underneath the header.

您可以使用部分来完成。如果我正确理解您的问题,您想要做的是,您必须在屏幕中间显示标题,同时在标题上方有一些单元格,在标题下方有一些单元格,并且当您向上滚动 tableView 时,您的标题应该滚动到顶部,当它到达顶部时,您希望标题保持在顶部,而单元格应滚动到标题下方。

So to do this return two section from the data source

所以要做到这一点,从数据源返回两个部分

func numberOfSectionsInTableView(tableView: UITableView) -> Int
{
     return 2
}

And in the data source return conditional cells

并在数据源中返回条件单元格

func tableView(tableView: UITableView, numberOfRowsInSection section:    Int) -> Int
{
    If(section == 1) {
      return <Return number of cell you want to display above header>
    } else {
      return <Return number of cell you want to display below header>
    }
}

Then override the delegate

然后覆盖委托

func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?
{
    if(section == 0) {
        return nil //As you do not have to display header in 0th section, return nil
    } else {
        return <Return your header view>

       //You can design your header view inside tableView as you design cell normally, set identifier to it and dequeue cell/headerView as:
       //However this may result in unexpected behavior since this is not how the tableview expects you to use the cells.

        let reuseIdentifier : String!
    reuseIdentifier = String(format: "HeaderCell")

        let headerView = tableView.dequeueReusableCellWithIdentifier(reuseIdentifier, forIndexPath: NSIndexPath(forRow: 0, inSection: 0))

        return headerView
    }
}

Set conditional height as

将条件高度设置为

func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat
{
    if(section == 0) {
        return 0 
    } else {
        return <Return your header view height>
    }
}

What we are doing here is, we are displaying header in second section which would have some cell, so you will see some cells in the tableView without header and below this cells you will see headerView with some cells and when you scroll your tableView up headerView will scroll with the cell till it reaches at the top.

我们在这里做的是,我们在第二部分显示标题,其中会有一些单元格,所以你会在 tableView 中看到一些没有标题的单元格,在这个单元格下面你会看到带有一些单元格的 headerView,当你向上滚动 tableView 时 headerView将与单元格一起滚动,直到它到达顶部。

Hope this will help you.

希望这会帮助你。