也许我的问题重演了。但没有一个答案没有帮助mea。
现在我在每个静态单元格中都有UITableViewController和静态单元格以及不同的rowHeight。
我有UIButton谁应该帮我透露我的UILabel中的全文。
我的屏幕拍摄我正在谈论的内容。
我的代码:
class DetailTableViewController: UITableViewController {
@IBOutlet weak var imagesCollectionView: UICollectionView!
@IBOutlet weak var conveniencesCollectionView: UICollectionView!
@IBOutlet weak var equipmentAndOtherCollectionView: UICollectionView!
@IBOutlet weak var descriptionLabel: UILabel!
@IBOutlet weak var readMoreButton: UIButton!
var hall: Halls?
let eq = ["Без проходной", "Циклорама", "Дневной свет", "Условия для семинаров", "Трехфазная нагрузка", "Генераторный свет", "Моноблоки", "Системы крепления"]
let con = ["Wi-Fi", "Платная парковка", "Кофе-машина", "Душ", "Организация мероприятий"] // [""]
var readMore: Bool = false
override func viewDidLoad() {
super.viewDidLoad()
tableView.tableFooterView = UIView()
tableView.estimatedRowHeight = 50
tableView.rowHeight = UITableViewAutomaticDimension
descriptionLabel.text = hall.description
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
switch section {
case 0: return 1
case 1: return 1
case 2: if eq.isEmpty || eq == [""] { return 0 } else { return 1 }
case 3: if con.isEmpty || con == [""] { return 0 } else { return 1 }
default: return 1
}
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.tableView.deselectRow(at: indexPath, animated: true)
}
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 0
}
@IBAction func readMoreButtonPressed(_ sender: UIButton) {
readMore = true
readMoreButton.isHidden = true
//... code for reveal text
}
}
hall.description有文字Пространство рассчитано на различные виды съёмок. Также возможно проведение различных мастер-классов, семинаров, встреч и мероприятий. Профессиональное оборудование Profoto D1 500 Air (4 источника) и крепкими стойками Manfrotto. Великолепная акустика. Крепкая белоснежная циклорама с регулируемым подогревом пола.2 больших окна, дающие великолепный дневной жесткий и мягкий свет (солнечная сторона). Аудиосистема с USB и AUX. Уникальные декорации в LOFT стиле. Бесплатное гримерное место перед съемкой.Бесплатный wi-fi.
@Dmitry Denikayev。
我找到了解决方案。你可以查看我的工作演示项目here.。
1)你需要设置UILabel
属性setNumberOfLines = 0
。
2)为视图增加和减少创建两个@IBOutlet
约束并设置其priority
。恩。 priority = 750
和priority = 250
(反之亦然)。
**查看以下代码**
在ViewTableViewCell.swift中
在ViewController.swift中
import UIKit
class ViewTableViewCell: UITableViewCell {
@IBOutlet var fixedHeightCon : NSLayoutConstraint!
@IBOutlet var graterHeightCon : NSLayoutConstraint!
@IBOutlet weak var lblText : UILabel!
@IBOutlet weak var btnMore: UIButton!
override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
我希望这个答案对你有帮助。快乐编码:)
如果你将 import UIKit
class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return arrData.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell : ViewTableViewCell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! ViewTableViewCell
cell.btnMore.tag = indexPath.row
cell.lblText.text = arrData[indexPath.row]
cell.layoutSubviews()
return cell
}
@IBOutlet weak var tblView: UITableView!
var arrData = ["This is long description.This is long description.This is long description.This is long description.This is long description.This is long description.This is long description.This is long description.This is long description.This is long description.This is long description.This is long description.This is long description.This is long description.","This is long description123.This is long description123.This is long description123.This is long description123.This is long description123.This is long description123.This is long description123.This is long description123.This is long description123.This is long description123.This is long description123.This is long description123.This is long description123."]
override func viewDidLoad() {
super.viewDidLoad()
tblView.tableFooterView = UIView()
tblView.rowHeight = UITableView.automaticDimension
tblView.estimatedRowHeight = 77
tblView.delegate = self
tblView.dataSource = self
// Do any additional setup after loading the view, typically from a nib.
}
@IBAction func changelabelHeight(sender:UIButton){
let indexpath = NSIndexPath(row: sender.tag, section: 0)
let cell = self.tblView!.cellForRow(at: indexpath as IndexPath) as? ViewTableViewCell
if(cell!.fixedHeightCon.priority == UILayoutPriority(rawValue: 750)){
cell!.btnMore.setTitle("Show Less", for: UIControl.State.normal)
cell!.fixedHeightCon.priority = UILayoutPriority(rawValue: 250)
cell!.graterHeightCon.priority = UILayoutPriority(rawValue: 750)
}else{
cell!.btnMore.setTitle("Read More", for: UIControl.State.normal)
cell!.fixedHeightCon.priority = UILayoutPriority(rawValue: 750)
cell!.graterHeightCon.priority = UILayoutPriority(rawValue: 250)
}
tblView.reloadData()
}
}
上的.amountOfLines
属性设置为5,那将自动截断字符串以适应5行。然后,当用户点击“阅读更多”按钮时,将其更改为0以允许UILabel
具有无限行以显示整个文本。如果删除放在单元格上的高度限制并正确设置自动布局,它将自动缩放。
另外,如果你想扩展UILabel动画,你可以在这里找到解决方案:UILabel