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