Подтвердить что ты не робот

Swift Добавить нижний колонтитул в UITableView

Это фактически таблица tableview и tableview, и я хотел добавить кнопку Submit после окончания ячейки tableview, но как это сделать?

Я попытался сделать это на раскадровке, добавив кнопку вручную, но ее не работает, кнопка не отображается. Есть ли другой способ сделать это?

Мне хотелось сделать снимок экрана ниже.

utmYR.jpg

4b9b3361

Ответ 1

Использование StoryBoard

В TableView Вы можете перетащить UIView, он будет установлен как FooterView, если у вас больше ячейки прототипа 0. После Drag вы можете увидеть его в иерархии tableview как subview. Теперь вы можете добавить кнопку ярлыка на этом представлении, вы также можете установить IBAction в файл класса 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)

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")
}

Ответ 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
}

Ответ 4

Добавьте 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))

Ответ 5

Свифт 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
}