Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableViewHeaderView defaulting to hang over cell, not above

I'm using UITableView's viewForHeaderInSection method, but I've decided the that the view would llook nicer with a lowered opacity over the cell by default, where it starts like so:

how it defaults

I'd like it to default like this, as if the user had already scrolled down (note, I want this for all cells, not just the one currently at the top of the view).

how i'd like it

I could just add the view as a subview of the cell, except I want it to maintain the functionality where, if the cell is the top cell, the header view stays atop the screen. Maybe the proper way to do this is to not have a headerview, and if a cell is the top cell calculate where the view's origin.y on the cell - although this seems like a potentially expensive way to go about it, it may be the only / best.

Thanks.

PSI'm looking to do this in swift, but objective C would do. Thanks again

like image 949
Ryan Avatar asked Jun 24 '14 02:06

Ryan


People also ask

What is uitableviewheaderfooterview in iOS?

Introduced in iOS 6, UITableViewHeaderFooterView takes the reuse functionality of table view cells and makes it available to section headers and footers. You can either use it directly or create a subclass to customize its appearance.

How to prototype section header/footer views as table view cells?

Dynamic table view cells can be designed directly from a Storyboard, which can be quite convenient for prototyping interfaces. Unfortunately, at the time of writing, there is no documented way to design prototype section header / footer views as you can with table view cells.

What is estimatedheaderheight in UITableView?

UITableView.automaticDimension is what enables AutoLayout to calculate the height of each cell at runtime. estimatedHeaderHeight should be set to a rough estimate of the total height of its content to display the scrolling indicator. These properties can be set in the Size Inspector when selecting the TableView in your storyboard.

What is uitableviewcell?

UITableViewCell is a subclass of UIView. This means we can use our storyboard to create a prototype cell which will be dequeued and returned from this function.


1 Answers

Is this what you want?

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--
            }
        }
    }
}
like image 119
Essan Parto Avatar answered Oct 19 '22 02:10

Essan Parto