Correctly returning UIView in viewForHeaderInSecti

2019-02-07 01:38发布

问题:

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!

回答1:

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
    }


回答2:

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
}