2016-03-02 57 views
2

我想创建一个tableview,它在tableview中间开始标题,然后可以用添加的tableView单元格向上滚动到顶部停止,然后tableview单元格可以滚动“下”它。我得到的标题是在tableview的中间,但tableView单元格只是滚动它的顶部,并且标题并不真正滚动到顶部。它只是留在原地。我希望能够让标题在滚动列表时移动,然后在它到达列表顶部时停止,然后它就是随后移动的tableView单元格。我如何保持标题单元格与Swift 2.0中的tableview单元格一起移动

我使用的是最新版本的XCode

+0

请分享到目前为止你做了什么(以及如何)。 – mobiuseng

回答

1

您可以使用章节进行操作。如果我正确理解你的问题,你想要做的是,你必须在屏幕中间显示标题,同时在标题上方有一些单元格和标题下面的一些单元格,当你滚动tableView时,应该滚动标题到顶部,当它到达顶部时,您希望头部保持在顶部,而单元格应在头部下方滚动。

因此,要做到这一点回报两条从数据源

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

,并在数据源返回条件细胞

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> 
    } 
} 

然后重写委托

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 
    } 
} 

设置条件身高为

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

我们在这里做的是,我们在第二部分显示头部有一些单元格,所以你会在tableView中看到一些没有头部的单元格,在这些单元格下面你会看到headerView和一些单元格,当你滚动你的tableView headerView将滚动单元格直到它到达顶部。

希望这会帮助你。

12

GO TO故事板>选择查看> SELECT泰伯维>属性检查器> CHNAGE STYPE从平原

看电邮宣传图片: enter image description here

+0

请随时评论为反对票,我将不胜感激 –

+1

让我怎么不是帮助你,我可能能够帮助你 –

+1

我没有投票给你,但这是一个贫穷的例子回答。首先,您的指示全部大写,并且有多个拼写错误。但更重要的是,你的指示并没有描述读者期望他们工作的原因。分组风格是什么会改变标题行为? –

相关问题