在viewForHeaderInSection

在viewForHeaderInSection

本文介绍了在viewForHeaderInSection iOS 8 swift中正确返回UIView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于快速语言和iOS开发整体来说,我是一个新手,所以请原谅我缺乏基础知识。以前我尝试并成功实现了多个UITableView自定义部分,通过创建一个xib文件创建TableViewCell,然后将其加载到我的主ViewController中并按下面的代码返回:

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

但是因为我开始得到没有索引表格单元格被重用的路径我回到绘图板并尝试以编程方式创建UIView并返回它,到目前为止我没有成功,但这是我编码的:

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/2,但是在您的约束中,您有80个点的空间,因此不能工作。在任何情况下,使用自动布局时都不应设置任何帧。摆脱它们,并为标签或按钮添加垂直约束,并使用布局选项垂直对齐它们。此外,您需要将标签和按钮添加到视图中(并在添加约束之前执行此操作)。这样的东西应该可以工作,

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
    }

这篇关于在viewForHeaderInSection iOS 8 swift中正确返回UIView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 19:33