Swift Add Footer View In UITableView

后端 未结 4 1835
长情又很酷
长情又很酷 2020-12-08 19:41

This is actually a tableview and tableview cell, and i wanted to add a Submit button after the end of the tableview cell, but how do i do it?

I tried to do it at th

4条回答
  •  猫巷女王i
    2020-12-08 20:02

    Swift 3/4

    1. If you want to add Table footer which is only visible at the end of 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. If you want to add section footer which is visible while scrolling through section.

    Adopt UITableViewDelegate and implement following delegate method.

    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
    }
    

提交回复
热议问题