如何使UITableView的第一部分以编程方式与顶部边框相距一定距离?

时间:2016-02-02 06:36:46

标签: ios swift uitableview

    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

    }

`

我是swift的新手,我希望让我的UITableView的第一部分显示在距离顶部边框一定距离处。我需要这样做以容纳两个按钮。 我现在面临的问题是我的按钮与我的第一部分重叠。

Image

3 个答案:

答案 0 :(得分:0)

实施tableView:heightForHeaderInSection并返回第0部分的标题视图的高度,根据您的代码为40。

答案 1 :(得分:0)

我找到了答案。我做的是返回

tableView: heightForHeaderInSection 

在第0部分为100,剩下的我保持默认为40.我为第0部分制作了dunamicButton框架

dunamicButton.frame = CGRectMake(0, 60, 377, 30)

The sneaky section 0 that was hiding finally showed up

愿部队与你在一起!

答案 2 :(得分:0)

您需要使用此方法

  • (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;

。设置高度后,您可以使用此方法

  • (可为空的UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;

    有约束。你想要从顶部边界到多少距离。