【问题标题】:UITableViewHeaderView defaulting to hang over cell, not aboveUITableViewHeaderView 默认挂在单元格上,而不是上方
【发布时间】:2014-06-24 02:05:55
【问题描述】:

我正在使用 UITableView 的 viewForHeaderInSection 方法,但我已经确定视图会更好,因为默认情况下会降低单元格上的不透明度,它的开头如下所示:

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

我可以将视图添加为单元格的子视图,但我希望它保持功能,如果单元格是顶部单元格,则标题视图保持在屏幕顶部。也许正确的方法是没有标题视图,如果一个单元格是顶部单元格,则计算该单元格上视图的 origin.y 的位置 - 尽管这似乎是一种潜在的昂贵方法,但它可能是只/最好。

谢谢。

PSI 希望迅速做到这一点,但目标 C 可以做到。再次感谢

【问题讨论】:

  • 有人做到了吗?我还在苦苦挣扎
  • 对所有单元格意味着什么?为什么要为所有单元格设置半透明标题?从 viewForHeaderInSection 返回不透明度降低的视图有什么问题?如果用户已经向下滚动你想要重现的行为呢?是我的解释还是你的解释?大声笑
  • 听起来你只是想要一个透明的普通 UITableViewStyle 标题?
  • 对不起,我不够清楚。标头默认高于实际的 UITableViewCell,而我希望它与 UITableViewCell 的视图的 origin.y 共享它的 origin.y。基本上,标题永远不会“高于”单元格,它只是从单元格的顶部开始,而不是在其上方。重温我发布的图片可能会进一步了解我的意图。
  • 是的...这真是令人困惑,看起来您正在对单元格背景视图做一些时髦的事情,使其看起来与顶部单元格连续...当您滚动经过顶部单元格时会发生什么这部分?也许你应该上传更多的图片来描述你想要什么。

标签: uitableview uiscrollview swift


【解决方案1】:

这是你想要的吗?

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

【讨论】:

  • 这不会编译并跟随代码我不认为这会默认我希望的方式。谢谢你的尝试
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-25
相关资源
最近更新 更多