I have a question.  

I have a UITableView and a custom UITableViewCell.  

First, I'll check the UILabel which in custom UITableViewCell whether is truncated or not.
If UILabel is trencated, I'll show the UIButton "show more", and set the UILabel numberOfLines equal to 2.  

When I click the UIButton "show more", I set the UILabel numberOfLines equal to 0, and the UIButton title change to "Close".  

The height of UITableViewCell is accroding to expanding UILabel content.  

If UILabel isn't trencated, I don't show the UIButton and set UILabel numberOfLines equal to 0.  

The height of UITableViewCell is also accroding to expanding UILabel content.  

How to acheive this situation?  

Thanks.  

How to remove red square area in following pic?

[![image](https://i.stack.imgur.com/jefI1m.png)](https://i.stack.imgur.com/jefI1m.png)

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

```