Как изменить макет и настройки свойства в табличном представлении willDisplay с функцией? - PullRequest
0 голосов
/ 27 декабря 2018

У меня есть вопрос.
У меня есть UITableView и пользовательский UITableViewCell.
Сначала я проверю UILabel, которая в пользовательском UITableViewCell урезана или нет.Если UILabel будет изменен, я покажу UIButton «показать больше» и установлю число UILabel numberOfLines равным 2.
Когда я нажимаю UIButton «показать больше», я устанавливаю UILabel numberOfLines равным 0, и UIButtonизменение заголовка на «Закрыть».
Высота UITableViewCell приближается к расширению содержимого UILabel.
Если UILabel не изменен, я не показываю UIButton и не устанавливаю UILabel numberOfLines равным 0.
Высота UITableViewCell также способствует расширению содержимого UILabel.
Как добиться этой ситуации?
Спасибо.

Как удалить область красного квадрата на следующем рисунке?

image

class ViewController: UIViewController {

    let itemCount: Int = 10
    let tableView = UITableView()
    let cellWithButton = "cellWithButton"
    var isExpand: Bool = false
    var expandingStateArray: [Bool] = []

    let textArray: [String] = ["If you read and listen to two articles every day, your reading and listening skills can immm If you read and listen to two articles every day, your reading and listening skills can immm If you read and listen to two articles every day, your reading and listening skills can immm If you read and listen to two articles every day, your reading and listening skills can immm If you read and listen to two articles every day, your reading and listening skills can immm", "If you read and listen to two", "If you read and listen to two articles every day, your reading and listening skills can immm If you read and listen to two articles every day", "If you read and listen to two articles every day, your reading and listening skills can immm If you read and listen to two articles every day", "If you read and listen to two articles every day, your reading and listening skills can immm If you read and listen to two articles every day", "If you read and listen to two articles every day, your reading and listening skills can immm If you read and listen to two articles every day", "If you read and listen to two articles every day, your reading and listening skills can immm If you read and listen to two articles every day", "If you read and listen to two articles every day, your reading and listening skills can immm If you read and listen to two articles every day", "If you read and listen to two articles every day, your reading and listening skills", "If you read and listen to two articles every day, your reading and listening skills can immm If you read and listen to two articles every day, your reading and listening skills can immm If you read and listen to two articles every day, your reading and listening skills can immm If you read and listen to two articles every day, your reading and listening skills can immm If you read and listen to two articles every day, your reading and listening skills can immm"]

    override func viewDidLoad() {
        super.viewDidLoad()

        for _ in 0...itemCount-1 {
            let bool = false
            expandingStateArray.append(bool)
        }

        tableView.delegate = self
        tableView.dataSource = self
        tableView.allowsSelection = false
        tableView.separatorInset = .zero
        tableView.estimatedRowHeight = 44
        tableView.rowHeight = UITableViewAutomaticDimension
        tableView.register(WithButtonTableViewCell.self, forCellReuseIdentifier: cellWithButton)

        self.view.addSubview(tableView)

        tableView.snp.makeConstraints { (make) in
            make.top.left.right.bottom.equalToSuperview()
        }
    }

    @objc func btnPressed(sender: UIButton) {

        let indexPath = IndexPath(row: sender.tag, section: 0)

        if self.isExpand == false {

            self.isExpand = true

            expandingStateArray[sender.tag] = true

        } else {
            self.isExpand = false

            expandingStateArray[sender.tag] = false
        }

        tableView.beginUpdates()
        tableView.reloadRows(at: [indexPath], with: .none)
        tableView.endUpdates()

    }


}

extension ViewController: UITableViewDelegate, UITableViewDataSource {

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return itemCount
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: cellWithButton, for: indexPath) as! WithButtonTableViewCell

        cell.titleLabel.text = textArray[indexPath.row]
        cell.expandButton.addTarget(self, action: #selector(btnPressed), for: .touchUpInside)
        cell.expandButton.tag = indexPath.row

        if expandingStateArray[indexPath.row] {
            cell.titleLabel.numberOfLines = 0
            cell.expandButton.setTitle("Close.", for: .normal)
        }else{
            cell.titleLabel.numberOfLines = 2
            cell.expandButton.setTitle("Show More.", for: .normal)
        }

        return cell
    }

    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {

        if let btnCell = cell as? WithButtonTableViewCell  {

            let labelIsTruncated: Bool = btnCell.titleLabel.isTruncated()

            btnCell.expandButton.isHidden = !labelIsTruncated
        }
    }
}

extension UILabel {

    func countLabelLines() -> Int {
        self.layoutIfNeeded()
        let myText = self.text! as NSString
        let attributes = [NSAttributedStringKey.font : self.font!]

        let labelSize = myText.boundingRect(with: CGSize(width: self.bounds.width, height: CGFloat.greatestFiniteMagnitude), options: NSStringDrawingOptions.usesLineFragmentOrigin, attributes: attributes, context: nil)
        return Int(ceil(CGFloat(labelSize.height) / self.font.lineHeight))
    }

    func isTruncated() -> Bool {

        if (self.countLabelLines() > self.numberOfLines) {
            return true
        }
        return false
    }
}


import UIKit

class WithButtonTableViewCell: UITableViewCell {

    var cellIsExpand: Bool = false

    let titleLabel: UILabel = { () -> UILabel in
        let ui = UILabel()
        ui.textColor = UIColor.black
        ui.numberOfLines = 2
        return ui
    }()

    let expandButton: UIButton = { () -> UIButton in
        let ui = UIButton()
        ui.setTitleColor(UIColor.blue, for: .normal)
        return ui
    }()

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)

        loadUI()
        loadLayout()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")

    }

    func loadUI() {

        self.addSubview(titleLabel)
        self.addSubview(expandButton)
    }

    func loadLayout() {

        titleLabel.snp.makeConstraints { (make) in
            make.top.left.equalTo(15)
            make.right.equalTo(-15)
        }

        expandButton.snp.makeConstraints { (make) in
            make.top.equalTo(titleLabel.snp.bottom).offset(10)
            make.left.equalTo(10)
            make.right.equalTo(-15)
            make.bottom.equalTo(-15)            
        }
    }
}

Ответы [ 3 ]

0 голосов
/ 27 декабря 2018

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

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: cellWithButton, for: indexPath) as! WithButtonTableViewCell

    cell.titleLabel.text = textArray[indexPath.row]
    cell.expandButton.addTarget(self, action: #selector(btnPressed), for: .touchUpInside)
    cell.expandButton.tag = indexPath.row

    if expandingStateArray[indexPath.row] {
        cell.titleLabel.numberOfLines = 0
        cell.expandButton.setTitle("Close.", for: .normal)
    }else{
        cell.titleLabel.numberOfLines = 2
        cell.expandButton.setTitle("Show More.", for: .normal)
    }

    let btnCell = cell
    let labelIsTruncated: Bool = btnCell.titleLabel.isTruncated()
    if !labelIsTruncated {
        btnCell.expandButton.snp.updateConstraints { (make) in
            make.height.equalTo(0)
        }
    }
    btnCell.expandButton.isHidden = !labelIsTruncated

    return cell
}

func loadLayout() {

    titleLabel.snp.makeConstraints { (make) in
        make.top.left.equalTo(15)
        make.right.equalTo(-15)
    }

    expandButton.snp.makeConstraints { (make) in
        make.top.equalTo(titleLabel.snp.bottom).offset(10)
        make.left.equalTo(10)
        make.right.equalTo(-15)
        make.bottom.equalTo(-15)
        make.height.equalTo(100)
    }
}

Это работает.И последнее: вы не должны изменять макет подпредставлений ячейки в методе cellDisplay.

Устаревший: Я думаю, что это проблема стратегии автоматического макета, вы устанавливаете макет метки и кнопки ниже:

    func loadLayout() {

    titleLabel.snp.makeConstraints { (make) in
        make.top.left.equalTo(15)
        make.right.equalTo(-15)
    }

    expandButton.snp.makeConstraints { (make) in
        make.top.equalTo(titleLabel.snp.bottom).offset(10)
        make.left.equalTo(10)
        make.right.equalTo(-15)
        make.bottom.equalTo(-15)
    }
}

Он будет вызываться только при инициализации ячейки, а также при обновлении табличного представления и скрытой кнопке ограничение не будет изменено.Я рекомендую вам рассчитать высоту этой ячейки вручную.Так как вы используете автоматическую высоту ячеек, должен быть какой-то продвинутый способ для поддержания собственного размера.Я также изучаю эту часть, и я скажу вам, когда я найду идеальный способ.Я надеюсь, что это может быть полезно для вас.

0 голосов
/ 27 декабря 2018

Установите ограничение высоты для кнопки «Показать больше» и установите его равным 0, если вы не хотите его показывать.

0 голосов
/ 27 декабря 2018

Я считаю, что это идеальная работа для UIStackView, так как установка

self.expandButton.isHidden = true

приведет к объединению контента, в противном случае перехватите ограничение высоты кнопки и установите его на

btnCell.btnHCon?.constant = 0
btnCell.layoutIfNeeded()

когда ты хочешь это скрыть


var btnHCon: Constraint? = nil

expandButton.snp.makeConstraints { (make) in
    make.top.equalTo(titleLabel.snp.bottom).offset(10)
    make.left.equalTo(10)
    make.right.equalTo(-15)
    make.bottom.equalTo(-15)  
    self.btnHCon = make.height.equalTo(40).constraint

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