Swift在UITableView中添加页脚视图

时间:2016-07-04 06:54:11

标签: ios iphone swift uitableview storyboard

这实际上是一个tableview和tableview单元格,我想在tableview单元格结束后添加一个Submit按钮,但我该怎么做呢?

我试图在故事板上手动添加按钮,但它不起作用,按钮没有显示。还有其他办法吗?

我想按照下面的截图进行操作。

4 个答案:

答案 0 :(得分:55)

使用StoryBoard

在TableView中您可以拖动UIView,如果您有超过0个原型单元格,它将设置为FooterView。拖动之后,你可以在tableview层次结构中看到它作为subview.Now,你可以在该View上添加标签按钮,你也可以将IBAction设置为ViewController类文件。

<强>编程

遵循3个步骤

  1. 使用按钮
  2. 创建一个自定义视图

    Swift 3.X / Swift 4.X

    android:windowSoftInputMode="adjustPan"

    Swift 2.X

    let customView = UIView(frame: CGRect(x: 0, y: 0, width: 200, height: 50))
    customView.backgroundColor = UIColor.red
    let button = UIButton(frame: CGRect(x: 0, y: 0, width: 100, height: 50))
    button.setTitle("Submit", for: .normal)
    button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
    customView.addSubview(button)
    
    1. 在表格页脚视图中添加该视图。
    2. Swift 2.X / Swift 3.X / Swift 4.X

      let customView = UIView(frame: CGRectMake(0, 0, 200, 50))
      customView.backgroundColor = UIColor.redColor()
      let button = UIButton(frame: CGRect(x: 0, y: 0, width: 100, height: 50))
      button.setTitle("Submit", forState: .Normal)
      button.addTarget(self, action: #selector(buttonAction), forControlEvents: .TouchUpInside)
      customView.addSubview(button)
      
      1. 您可以对同一课程中的该按钮执行操作。
      2. Swift 3.X / Swift 4.X

        myTblView.tableFooterView = customView
        

        Swift 2.X

        @objc func buttonAction(_ sender: UIButton!) {
            print("Button tapped")
        }
        

答案 1 :(得分:6)

func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
    let footerView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.size.width, height: 50))
    return footerView
}

func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
    return 50
}

答案 2 :(得分:1)

迅速添加UILabel作为UITableView.Footer

    let footerView:UILabel = UILabel(frame: CGRect(x: 0, y: 0, width:320 , height: 500))
    footerView.text = "add description ro Timevc in order user will see the result of lesson time"
    footerView.numberOfLines = 0;
    footerView.sizeToFit()
    tableView.tableFooterView = footerView
    tableView.contentInset = (UIEdgeInsetsMake(0, 8, -footerView.frame.size.height, 8))

答案 3 :(得分:1)

快速3/4

1。如果要添加仅在TabelView末尾可见的表格页脚

let customView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.width, height: 150))
customView.backgroundColor = UIColor.clear
let titleLabel = UILabel(frame: CGRect(x:10,y: 5 ,width:customView.frame.width,height:150))
titleLabel.numberOfLines = 0;
titleLabel.lineBreakMode = .byWordWrapping
titleLabel.backgroundColor = UIColor.clear
titleLabel.textColor = PTConstants.colors.darkGray
titleLabel.font = UIFont(name: "Montserrat-Regular", size: 12)
titleLabel.text  = "Payment will be charged to your Apple ID account at the confirmation of purchase. Subscription automatically renews unless it is canceled at least 24 hours before the end of the current period. Your account will be charged for renewal within 24 hours prior to the end of the current period. You can manage and cancel your subscriptions by going to your account settings on the App Store after purchase."
customView.addSubview(titleLabel)
tableView.tableFooterView = customView

2。如果您要添加在页眉中可见的页脚。

采用UITableViewDelegate并实现以下委托方法。

func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
    let vw = UIView()
    vw.backgroundColor = UIColor.clear
    let titleLabel = UILabel(frame: CGRect(x:10,y: 5 ,width:350,height:150))
    titleLabel.numberOfLines = 0;
    titleLabel.lineBreakMode = .byWordWrapping
    titleLabel.backgroundColor = UIColor.clear
    titleLabel.font = UIFont(name: "Montserrat-Regular", size: 12)
    titleLabel.text  = "Footer text here"
    vw.addSubview(titleLabel)
    return vw
}

func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
    return 150
}