【问题标题】:Correctly returning UIView in viewForHeaderInSection iOS 8 swift在 viewForHeaderInSection iOS 8 swift 中正确返回 UIView
【发布时间】:2015-02-11 12:21:57
【问题描述】:

我对 Swift 语言和整个 iOS 开发还很陌生,所以请原谅我缺乏基础知识。以前我尝试并成功地通过创建一个 xib 文件创建 TableViewCell 然后将其加载到我的主 ViewController 并按如下代码返回它来成功实现多个分段的 UITableView 自定义部分:

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

但是自从我开始得到“没有重复使用表格单元的索引路径”后,我回到绘图板并尝试以编程方式创建 UIView 并返回它,到目前为止我一直没有成功,但这就是我编码的:

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

如您所见,我正在尝试创建一个布局,我希望我的标签和按钮水平布局,但不知何故逻辑无法正常工作,我尝试禁用视图本身的自动调整大小约束,但这也没有奏效。请帮忙!

【问题讨论】:

    标签: ios iphone uitableview swift uiview


    【解决方案1】:

    您从未将标签或按钮添加到“查看”。此外,您的尺寸没有意义——您将标签和按钮的宽度设置为表格视图宽度的 1/2,但是在您的约束中,您有 80 个点的空间,所以不能工作。在任何情况下,您都不应在使用自动布局时设置任何框架。摆脱这些,并为标签或按钮添加垂直约束,以及垂直对齐它们的布局选项。此外,您需要将标签和按钮添加到视图中(并在添加约束之前执行此操作)。这样的事情应该可以工作,

    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
      }
      

      【讨论】:

        猜你喜欢
        • 2014-12-21
        • 2015-05-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-08-12
        • 2019-12-04
        相关资源
        最近更新 更多