UITableViewHeaderView默认挂起单元格,而不是挂起单元格上方

UITableViewHeaderView默认挂起单元格,而不是挂起单元格上方,uitableview,uiscrollview,swift,Uitableview,Uiscrollview,Swift,我正在使用UITableView的ViewForHeaderSection方法,但我已经决定,默认情况下,如果单元格上的不透明度降低,视图会更好,它的开头是这样的: 我希望它默认为这样,就像用户已经向下滚动一样(注意,我希望所有单元格都这样,而不仅仅是当前位于视图顶部的单元格) 我可以将视图添加为单元格的子视图,但我希望它能够保持功能,如果单元格是顶部单元格,则标题视图将保持在屏幕顶部。也许正确的方法是不使用headerview,如果单元格是顶部单元格,则计算视图在单元格上的原点.y-尽管这

我正在使用UITableView的ViewForHeaderSection方法,但我已经决定,默认情况下,如果单元格上的不透明度降低,视图会更好,它的开头是这样的:

我希望它默认为这样,就像用户已经向下滚动一样(注意,我希望所有单元格都这样,而不仅仅是当前位于视图顶部的单元格)

我可以将视图添加为单元格的子视图,但我希望它能够保持功能,如果单元格是顶部单元格,则标题视图将保持在屏幕顶部。也许正确的方法是不使用headerview,如果单元格是顶部单元格,则计算视图在单元格上的原点.y-尽管这似乎是一种可能代价高昂的方法,但它可能是唯一/最好的方法

谢谢

我希望用swift实现这一点,但目标C可以。再次感谢你这是你想要的吗

import UIKit

class ViewController: UITableViewController {
    var data = [[String]]()
    var headerOrigins = [CGPoint]()
    var floatingHeaders = [UIView]()

    override func viewDidLoad() {
        super.viewDidLoad()

        data = [["One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eigth", "Nine", "Ten"], ["One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eigth", "Nine", "Ten"], ["One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eigth", "Nine", "Ten"]]

        tableView.dataSource = self
        tableView.delegate = self

        for i in 0..<data.count {
            headerOrigins += tableView(tableView, cellForRowAtIndexPath: NSIndexPath(forRow: 0, inSection: i)).frame.origin

            let floatingHeader = UIView(frame: CGRectMake(0, headerOrigins[i].y, tableView.frame.size.width, 44))
            floatingHeader.backgroundColor = UIColor.redColor()
            floatingHeader.alpha = 0.5

            let label = UILabel(frame: CGRectMake(100, 10, 100, 30))
            label.text = "Header \(i)"
            floatingHeader.addSubview(label)

            tableView.addSubview(floatingHeader)

            floatingHeaders += floatingHeader
        }

    }

    override func numberOfSectionsInTableView(tableView: UITableView!) -> Int {
        return data.count
    }

    override func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {
        return data[section].count
    }

    override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
        let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
        cell.textLabel.text = data[indexPath.section][indexPath.row]

        return cell
    }

    override func scrollViewDidScroll(scrollView: UIScrollView!) {
        var tableTop = tableView.bounds.origin.y + tableView.scrollIndicatorInsets.top

        if tableTop < 0 {
            floatingHeaders[0].frame.origin.y = 0
        } else {
            for i in 0..<data.count {
                if tableTop > headerOrigins[i].y {
                    floatingHeaders[i].frame.origin.y = tableTop
                } else if tableTop < headerOrigins[i].y {
                    floatingHeaders[i].frame.origin.y = headerOrigins[i].y
                }

            var j = i
            while j > 0 {
                if (CGRectGetMaxY(floatingHeaders[j-1].frame) > floatingHeaders[j].frame.origin.y) {
                    floatingHeaders[j-1].frame.origin.y = floatingHeaders[j].frame.origin.y - floatingHeaders[j].frame.size.height
                }
                j--
            }
        }
    }
}
导入UIKit
类ViewController:UITableViewController{
变量数据=[[String]]()
var headerOrigins=[CGPoint]()
var floatingHeaders=[UIView]()
重写func viewDidLoad(){
super.viewDidLoad()
数据=[“一”、“二”、“三”、“四”、“五”、“六”、“七”、“八”、“九”、“十”]、[“一”、“二”、“三”、“四”、“五”、“六”、“七”、“八”、“九”、“十”]、[“一”、“二”、“三”、“四”、“五”、“六”、“七”、“八”、“九”、“十”]
tableView.dataSource=self
tableView.delegate=self
对于0..Int中的i{
返回数据.count
}
重写func tableView(tableView:UITableView!,numberofrowsinssection:Int)->Int{
返回数据[section]。计数
}
重写func tableView(tableView:UITableView!,cellForRowAtIndexPath:nsindepath!)->UITableViewCell{
将cell=tableView.dequeueReusableCellWithIdentifier(“cell”,forIndexPath:indexPath)设为UITableViewCell
cell.textlab.text=数据[indexPath.section][indexPath.row]
返回单元
}
重写func scrollViewDidScroll(scrollView:UIScrollView!){
var tableTop=tableView.bounds.origin.y+tableView.scrollIndicationSets.top
如果桌面<0{
浮动标头[0]。frame.origin.y=0
}否则{
对于0..headerOrigins[i].y中的i{
浮动头[i].frame.origin.y=桌面
}否则,如果桌面0{
if(CGRectGetMaxY(floatingHeaders[j-1].frame)>floatingHeaders[j].frame.origin.y){
浮动头[j-1]。frame.origin.y=浮动头[j]。frame.origin.y-浮动头[j]。frame.size.height
}
j--
}
}
}
}

有人做到了吗?我仍在努力解决这个问题。你对所有单元格的意思是什么?为什么你希望所有单元格都有一个半透明的标题?从viewForHeaderInSection返回一个不透明度降低的视图有什么错?如果用户已经向下滚动了,你想复制这个行为呢?是我还是你的经验lanation?lolit听起来你只是想要一个半透明的普通UITableViewStyle标题?很抱歉我不够清晰。标题默认位于实际的UITableViewCell之上,而我希望它与UITableViewCell的视图的原点共享它的原点。y。基本上,标题永远不会“在上方”单元格,它只是从单元格的顶部开始,而不是从上面开始。重温我发布的图片可能会进一步理解我的意图。是的……这太令人困惑了,看起来你正在做一些奇怪的事情,单元格背景视图使它看起来与顶部单元格连续……当你滚动过sec的顶部单元格时会发生什么tion?也许你应该上传更多的图片来描述你想要的东西。这不会编译并跟随代码,我不认为这会像我希望的那样默认。不过谢谢你的尝试