Добавление кнопки вместе с текстом в нижний колонтитул раздела в UITableView - PullRequest
0 голосов
/ 30 мая 2018

Я пытаюсь добавить нажимаемую кнопку вместе с текстом в нижний колонтитул раздела в UITableView.Я хочу добиться того же результата, что и приложение «Настройки iOS» (например, кнопка «Узнать больше ...» на снимке экрана).

Кто-нибудь знает, как Apple это делает?

enter image description here

EDIT

Извините, скриншот я добавилможет быть немного вводит в заблуждение, поскольку я держал кнопку, пока она выделялась.Вот как на самом деле выглядит нижний колонтитул:

enter image description here

Я попытался реализовать titleForFooterInSection и затем добавить распознаватель жестов к стандартной метке нижнего колонтитула в willDisplayFooterView, но наПервоначальный вызов метки равен нулю.Я не уверен, что это хороший подход в любом случае ..

override func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? {
    return NSLocalizedString("This is some random text.", comment: "") + NSLocalizedString("Refresh", comment: "")
}

override func tableView(_ tableView: UITableView, willDisplayFooterView view: UIView, forSection section: Int) {

    guard let label = view.subviews[1].subviews.first as? UILabel else {
        return
    }
    let text = label.text!
    let underlineAttriString = NSMutableAttributedString(string: text)
    let range = (text as NSString).range(of: NSLocalizedString("Refresh", comment: ""))
    underlineAttriString.addAttribute(.foregroundColor, value: UIColor.blue, range: range)
    label.attributedText = underlineAttriString
    view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(refreshTapped)))
}

1 Ответ

0 голосов
/ 30 мая 2018

Просто используйте viewForHeaderInSection

 override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 150
    }
    override func tableView(_ tableView: UITableView, viewForFooterInSection  section: Int) -> UIView? {

        let header  = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 150));

            header.backgroundColor = UIColor.groupTableViewBackground
        let label = UILabel.init(frame: CGRect.init(x: 10, y: 10, width: tableView.frame.width-20, height: 100))
        label.text = "I tried using your code, but the footer doesn't look like the one from my screenshot. Here's what I got: Screenshot I usually work with storyboards and xibs so I don't really know how to align the text and button"
        label.numberOfLines = 4
        header.addSubview(label)

        let button = UIButton.init(frame: CGRect.init(x: ((tableView.frame.width-100) / 2), y: 90, width: 100, height: 40))
        button.setTitle("LoadMore...", for: UIControlState.normal)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 12)
        button.setTitleColor(.black, for: UIControlState.normal)
        button.backgroundColor = .white
        button.clipsToBounds = true
        button.layer.cornerRadius = 20
        header.addSubview(button)

        return header

    }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...