您的位置:首页 > 移动开发 > Swift

iOS.swift 纯代码 关于如何提取TableViewCell视图作为view

2015-11-24 01:13 543 查看

需求很简单,例如我们的tableview有3个section,每个section又分别有3个row。然后当下拉到第三个section的时候,section=3&&row=0,卡在最上端,不再跟随移动。

首先,我们需要在TableViewCell里对section=3&&row=0写一个UIView,把相关的控件都放到这个UIView上。

然后,我们还需要定义一个UIView,放置在我们需要卡着的位置。例如:

headView.frame = CGRectMake(0, 64, self.view.frame.width, self.view.frame.height * 0.08)


其次,我们在下拉过程中。需要对方法scrollViewDidScroll进行操作。如下:

/**
headView是为我们预先准备好要卡住的位置
cellView是我们根据section,row从TabViewCell里提取的
bgView是在cell中设置为控件addsubview的视图
*/
func scrollViewDidScroll(scrollView: UIScrollView) {
let cellView = tableView.cellForRowAtIndexPath(NSIndexPath.init(forRow: 0, inSection: 3)) as? TableViewCell
if scrollView.contentOffset.y > self.view.frame.height * 0.6{
if cellView != nil{
headView.backgroundColor = UIColor.whiteColor()
cellView!.bgView.removeFromSuperview()
headView.hidden = false
headView.addSubview(cellView!.bgView)
}else{
headView.backgroundColor = UIColor.clearColor()
cellView?.addSubview((cellView?.bgView)!)
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  cell swift tableview