Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correctly returning UIView in viewForHeaderInSection iOS 8 swift

I am fairly new to swift language and iOS development as a whole so please pardon me for my lack of fundamental knowledge. Previously i tried and successfully Implemented multiple sectioned UITableView custom sections by making a xib file creating TableViewCell and then loading it into my main ViewController and returning it as coded below:

var customView = NSBundle.mainBundle().loadNibNamed("CustomHeader",owner: self, options: nil)[0] as? UIView
return customView

But since i started getting "no index path for table cell being reused" i went back to drawing board and tried to do things programmatically creating a UIView and return it, so far i am have been unsuccessful however this is what i coded:

func tableView(tableView: UITableView!, viewForHeaderInSection section: Int) -> UIView!{
    if(section == 0) {
        var view = UIView(frame: CGRectMake(0, 0, tableView.frame.size.width, 50))
        var label = UILabel(frame: CGRectMake(0,0, tableView.frame.size.width/2, 20))
        label.text="My Details"
        let button   = UIButton.buttonWithType(UIButtonType.System) as UIButton
        button.frame = CGRectMake(0, 0,  tableView.frame.size.width/2, 20)
        button.addTarget(self, action: "visibleRow", forControlEvents:.TouchUpInside)

        label.setTranslatesAutoresizingMaskIntoConstraints(false)
        button.setTranslatesAutoresizingMaskIntoConstraints(false)
        let views = ["label": label,"button":button,"view": view]    
        var horizontallayoutContraints = NSLayoutConstraint.constraintsWithVisualFormat("H:|-10-[label(20)]-60-[button(20)]-10-|", options: NSLayoutFormatOptions(0), metrics: nil, views: views)
        view.addConstraints(horizontallayoutContraints)

        return view
    }
...

As you can see i am trying to create a layout where i want my label and button horizontally laid out but somehow logic is not working out i tried disabling autoresize constraints on view itself but that too did not worked too. Please Help!

like image 201
yasir zamir Avatar asked Dec 12 '14 18:12

yasir zamir


2 Answers

You never added your label or button to "view". Also your sizes don't make sense -- you set the widths of the label and button to be 1/2 the width of the table view, but then in your constraints, you have 80 points worth of spaces, so that can't work. In any case, you should NOT set any frames when you use auto layout. Get rid of those, and add a vertical constraint to either the label or the button, and a layout option to align them vertically. Also, you need to add your label and button to your view (and do it before you add the constraints). Something like this should work,

func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

    if(section == 0) {
            var view = UIView() // The width will be the same as the cell, and the height should be set in tableView:heightForRowAtIndexPath:
            var label = UILabel()
            label.text="My Details"
            let button   = UIButton.buttonWithType(UIButtonType.System) as UIButton
            button.addTarget(self, action: "visibleRow:", forControlEvents:.TouchUpInside)
            label.setTranslatesAutoresizingMaskIntoConstraints(false)
            button.setTranslatesAutoresizingMaskIntoConstraints(false)
            button.setTitle("Test Title", forState: .Normal)
            let views = ["label": label,"button":button,"view": view]
            view.addSubview(label)
            view.addSubview(button)
            var horizontallayoutContraints = NSLayoutConstraint.constraintsWithVisualFormat("H:|-10-[label]-60-[button]-10-|", options: .AlignAllCenterY, metrics: nil, views: views)
            view.addConstraints(horizontallayoutContraints)

            var verticalLayoutContraint = NSLayoutConstraint(item: label, attribute: .CenterY, relatedBy: .Equal, toItem: view, attribute: .CenterY, multiplier: 1, constant: 0)
            view.addConstraint(verticalLayoutContraint)
            return view
        }
        return nil
    }


func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 50
    }
like image 88
rdelmar Avatar answered Sep 23 '22 03:09

rdelmar


override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return 20
}

override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let view = UIView()
    view.backgroundColor = UIColor.clearColor()
    return view
}
like image 35
Abo3atef Avatar answered Sep 20 '22 03:09

Abo3atef