【问题标题】:Swift Add Footer View In UITableViewSwift 在 UITableView 中添加页脚视图
【发布时间】:2016-11-05 19:40:04
【问题描述】:

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

我尝试在情节提要中手动添加按钮,但它不起作用,按钮没有显示。还有其他方法吗?

我想像下面的截图那样做。

【问题讨论】:

  • 您希望按钮作为 TableView 的内容可滚动或始终在 TableView 下方可见?
  • @ArnaudChrist 我希望它可以滚动

标签: ios iphone swift uitableview storyboard


【解决方案1】:

使用故事板

在 UITableView 中你可以拖动 UIView,如果你有超过 0 个原型单元格,它将设置为 FooterView。拖动后,您可以在表格视图层次结构中将其视为子视图。现在,您可以在该视图上添加标签按钮,也可以将 IBAction 设置为 ViewController 类文件。

以编程方式

遵循 3 个步骤

  1. 使用按钮制作一个自定义视图,

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)

Swift 2.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. 在表尾视图中添加该视图。

Swift 2.X/Swift 3.X/Swift 4.X

myTblView.tableFooterView = customView
  1. 您可以在同一类中对该按钮执行操作。

Swift 3.X/Swift 4.X

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

Swift 2.X

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

【讨论】:

  • 页脚,而不是页眉
  • @mimi93 我更新代码,只给背景颜色和更新框架。请检查
  • @Theorist 它仍然无法正常工作,是因为我的自定义表格视图单元格吗?
  • 能否请您也添加 headerView 的代码以完成..
  • @nyxee 这是相同的代码,只是更改行 myTblView.tableHeaderView = customView
【解决方案2】:
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
}

【讨论】:

  • 您能否为您的代码提供更多上下文?有 35 个赞成的答案已被接受,通过添加新答案,您应该告诉我们您的代码比其他代码更能解决什么
  • 优秀的答案。 @NicoHaase 那家伙想帮忙,天哪。
  • 节中的页脚和表格视图页脚是两个不同的东西。第一个将在滚动部分时可见,但后者将添加到 tableview 的末尾并且仅在末尾可见。
【解决方案3】:

斯威夫特 3/4

1.如果要添加仅在 TabelView 末尾可见的 Table 页脚

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
}

【讨论】:

  • 仅在 TabelView 末尾可见的添加表格页脚效果很好。谢谢!
【解决方案4】:

在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))

【讨论】:

  • UILabel 的宽度是硬编码的。请根据屏幕尺寸使其动态化
【解决方案5】:

// 用于标题

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let HeaderView = UIView(frame: CGRect(x: 0, y: 0, width: TableViews.frame.size.width, height: 10))
        HeaderView.backgroundColor = .green
        let HeaderTitle = UILabel(frame: CGRect(x: 10, y: 10, width: TableViews.frame.size.width, height: 20))
        HeaderTitle.text = "Hi You Welcome"
        HeaderView.addSubview(HeaderTitle)
        return HeaderView
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { 40 }
   
    // for footer 

    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        let Footer = UIView()
        Footer.backgroundColor = UIColor.green
        let titleLabel = UILabel(frame: CGRect(x:10,y: 5 ,width:350,height:30))
        titleLabel.textColor = .blue
        titleLabel.text  = "Hi I am Muhammad Hassan"
        Footer.addSubview(titleLabel)
        return Footer
    }

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

...这是页眉和页脚的最佳功能...

【讨论】:

    猜你喜欢
    • 2015-05-23
    • 1970-01-01
    • 2012-07-11
    • 2021-07-06
    • 1970-01-01
    • 1970-01-01
    • 2011-01-31
    • 2010-12-26
    • 1970-01-01
    相关资源
    最近更新 更多