Swift在UITableView中添加页脚视图

mim*_*i93 27 iphone storyboard uitableview ios swift

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

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

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

Uje*_*esh 68

使用StoryBoard

在TableView中您可以拖动UIView,如果您有超过0个原型单元格,它将设置为FooterView.拖动之后,你可以在tableview层次结构中看到它作为subview.Now,你可以在该View上添加标签按钮,你也可以将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)
Run Code Online (Sandbox Code Playgroud)

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)
Run Code Online (Sandbox Code Playgroud)
  1. 在表格页脚视图中添加该视图.

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

myTblView.tableFooterView = customView
Run Code Online (Sandbox Code Playgroud)
  1. 你可以对同一个班级的那个按钮采取行动.

Swift 3.X/Swift 4.X

@objc func buttonAction(_ sender: UIButton!) {
    print("Button tapped")
}
Run Code Online (Sandbox Code Playgroud)

Swift 2.X

func buttonAction(sender: UIButton!) {
  print("Button tapped")
}
Run Code Online (Sandbox Code Playgroud)


小智 8

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
}
Run Code Online (Sandbox Code Playgroud)

  • section和tableview页脚中的页脚是两件不同的事情。滚动该部分时第一个可见,但是第二个将添加到tableview的末尾,并且仅在末尾可见。 (2认同)

Abh*_*man 7

迅捷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
Run Code Online (Sandbox Code Playgroud)

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
}
Run Code Online (Sandbox Code Playgroud)