func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    print("Inside viewForHeader")
    if(section > 0){

    let headerView = UIButton(frame: CGRectMake(0, 0, tableView.frame.size.width, 40))
    headerView.titleLabel?.text = myOrders.allmyorders[section].customerOrder.number
    let dunamicButton = UIButton(type : UIButtonType.System)
    dunamicButton.backgroundColor = UIColor.clearColor()
    dunamicButton.setTitle("Order number: \(myOrders.allmyorders[section].customerOrder.number)", forState: UIControlState.Normal)
    dunamicButton.tag = section
    dunamicButton.frame = headerView.frame
    dunamicButton.enabled = true
    dunamicButton.addTarget(self, action: "pressed:", forControlEvents: UIControlEvents.TouchUpInside)
    headerView.addSubview(dunamicButton)
    return headerView
    }


    else {
        let headerView = UIView(frame: CGRectMake(0, 0, tableView.frame.size.width, 40))
        let dunamicButton = UIButton(type : UIButtonType.System)
        dunamicButton.backgroundColor = UIColor.clearColor()
        dunamicButton.setTitle("Order number: \(myOrders.allmyorders[section].customerOrder.number)", forState: UIControlState.Normal)
        dunamicButton.tag = section
        dunamicButton.frame = CGRectMake(0, 0, 377, 30)
        dunamicButton.enabled = true
        dunamicButton.addTarget(self, action: "pressed:", forControlEvents: UIControlEvents.TouchUpInside)
        headerView.addSubview(dunamicButton)

        headerView.backgroundColor = UIColor.blackColor()
        let buttonActive = UIButton(type : UIButtonType.System) as! UIButton
        buttonActive.backgroundColor = UIColor.blueColor()
        buttonActive.layer.cornerRadius = 5
        buttonActive.layer.borderWidth = 1
        buttonActive.alpha = 0.3
        buttonActive.layer.borderColor = UIColor.whiteColor().CGColor
        buttonActive.setTitle("Active Orders", forState: UIControlState.Normal)
        buttonActive.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
        buttonActive.frame = CGRectMake(0,0,self.view.frame.size.width/2,40)
        buttonActive.addTarget(self, action: "ActiveOrdersPressed", forControlEvents: .TouchUpInside)
        let buttonAllMyOrders = UIButton(type : UIButtonType.System) as! UIButton
        buttonAllMyOrders.backgroundColor = UIColor.blueColor()
        buttonAllMyOrders.setTitle("All My Orders", forState: UIControlState.Normal)
        buttonAllMyOrders.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
        buttonAllMyOrders.layer.cornerRadius = 5
        buttonAllMyOrders.layer.borderWidth = 1
        buttonAllMyOrders.alpha = 0.3
        buttonAllMyOrders.layer.borderColor = UIColor.whiteColor().CGColor
        buttonAllMyOrders.frame = CGRectMake(self.view.frame.size.width/2,0,self.view.frame.size.width/2,40)
        buttonAllMyOrders.addTarget(self, action: "AllMyOrdersOrdersPressed", forControlEvents: .TouchUpInside)
        headerView.backgroundColor = UIColor.clearColor()
        headerView.addSubview(buttonActive)
        headerView.addSubview(buttonAllMyOrders)

        return headerView

    }

`
我是新来的斯威夫特,我希望我的UITableView的第一部分出现在一定距离的顶部边界。我需要这样做以容纳两个按钮。
我现在面临的问题是我的按钮和我的第一部分重叠。
Image

最佳答案

实现tableView:heightForHeaderInSection并返回第0节头视图的高度,根据代码,该高度为40。

关于ios - 如何以编程方式使UITableView的第一部分显示在距顶部边框一定距离的地方?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35146655/

10-11 15:24