Ios 在UITableView中快速添加页脚视图

Ios 在UITableView中快速添加页脚视图,ios,iphone,swift,uitableview,storyboard,Ios,Iphone,Swift,Uitableview,Storyboard,这实际上是一个tableview和tableview单元格,我想在tableview单元格的末尾添加一个Submit按钮,但是我该怎么做呢 我试图在故事板上手动添加按钮,但它不工作,按钮不显示。还有别的办法吗 我想做下面的截图 使用故事板 在UITableView中,您可以拖动UIView,如果原型单元超过0,则它将设置为FooterView。拖动后,可以在表视图层次结构中将其作为子视图查看。现在,您可以在该视图上添加标签按钮,还可以将iAction设置到ViewController类文件中 以

这实际上是一个tableview和tableview单元格,我想在tableview单元格的末尾添加一个Submit按钮,但是我该怎么做呢

我试图在故事板上手动添加按钮,但它不工作,按钮不显示。还有别的办法吗

我想做下面的截图


使用故事板

在UITableView中,您可以拖动UIView,如果原型单元超过0,则它将设置为FooterView。拖动后,可以在表视图层次结构中将其作为子视图查看。现在,您可以在该视图上添加标签按钮,还可以将iAction设置到ViewController类文件中

以编程方式

遵循3个步骤

  • 使用按钮创建一个自定义视图
  • Swift 3.X/Swift 4.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)
    
    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)
    
    myTblView.tableFooterView = customView
    
    @objc func buttonAction(_ sender: UIButton!) {
        print("Button tapped")
    }
    
    func buttonAction(sender: UIButton!) {
      print("Button tapped")
    }
    
    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)
    
    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)
    
    myTblView.tableFooterView = customView
    
    @objc func buttonAction(_ sender: UIButton!) {
        print("Button tapped")
    }
    
    func buttonAction(sender: UIButton!) {
      print("Button tapped")
    }
    
  • 在表页脚视图中添加该视图
  • Swift 2.X/Swift 3.X/Swift 4.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)
    
    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)
    
    myTblView.tableFooterView = customView
    
    @objc func buttonAction(_ sender: UIButton!) {
        print("Button tapped")
    }
    
    func buttonAction(sender: UIButton!) {
      print("Button tapped")
    }
    
  • 您可以在同一个类中对该按钮执行操作
  • Swift 3.X/Swift 4.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)
    
    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)
    
    myTblView.tableFooterView = customView
    
    @objc func buttonAction(_ sender: UIButton!) {
        print("Button tapped")
    }
    
    func buttonAction(sender: UIButton!) {
      print("Button tapped")
    }
    
    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)
    
    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)
    
    myTblView.tableFooterView = customView
    
    @objc func buttonAction(_ sender: UIButton!) {
        print("Button tapped")
    }
    
    func buttonAction(sender: UIButton!) {
      print("Button tapped")
    }
    

    在swift中将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))
    

    Swift 3/4

    1。如果要添加仅在选项卡视图末尾可见的表尾

    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
    }
    

    您希望该按钮作为TableView的内容可滚动,还是在TableView下方始终可见?@ArnaudChrist我希望它是可滚动的页脚,而不是header@mimi93我更新代码,只是给背景颜色和更新帧。请Check@Theorist它仍然不起作用,是因为我的自定义tableview单元格吗?能否请您也为headerView添加代码以完成..@nyxee它是相同的代码只是更改行myTblView.tableHeaderView=CustomView您能为代码提供更多上下文吗?有一个35票以上的公认答案,通过添加一个新答案,您应该告诉我们您的代码比其他优秀答案更好地解决了什么问题@尼科哈斯,那个家伙想帮忙,天啊,部分的页脚和tableview页脚是两个不同的东西。第一个在滚动部分时可见,但后一个将添加到tableview的末尾,并且仅在末尾可见。UILabel的宽度是硬编码的。请根据屏幕大小使其动态添加表格页脚,该页脚仅在选项卡“视图”的末尾可见。非常感谢。