阅读更多 UIButton 上的单元格高度增加问题
Issue in increase height of cell on Read more UIButton
也许我的问题重复了。但是没有一个答案对我没有帮助。
现在我有 UITableViewController 和 static cells 并且每个 static cells 的 rowHeight 不同。
我有 UIButton 可以帮助我在 UILabel.
中显示全文
- 第一行有 implement collectionView 和 height == 490.0
- 第二行在 UILabel 中有文本,我希望在单击 UIButton 时以全文显示,默认高度为 150.0 , 但如果文字会有很多文字,我需要更高的高度
- 第三行有实现collectionView和height == 150.0
- 第四行有implement collectionView and height == 150.0
- 第五行有UILabel和height == 50.0
还有我的屏幕截图我在说什么。
还有我的代码:
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.
如果您将 UILabel
上的 .amountOfLines
属性 设置为类似 5 的值,则会自动截断字符串以适合 5 行。然后当用户点击阅读更多按钮时,将其更改为 0 以允许 UILabel
有无限行来显示整个文本。如果您取消对单元格设置的高度限制并正确设置自动布局,它会自动缩放。
此外,如果您希望 UILabel 的展开具有动画效果,您可以在此处找到该解决方案:
@Дмитрий Деникаев。
我找到了解决方案。你可以查看我的工作演示项目here.。
1) 你需要设置UILabel
属性 setNumberOfLines = 0
.
2) 创建两个@IBOutlet
视图增加和减少约束并设置其priority
。前任。 priority = 750
和 priority = 250
(反之亦然)。
标签固定高度的第一个约束,它在故事板中的优先级是 750。
标签底部对其父视图的第二个约束,它在故事板中的优先级是 250。
**查看以下代码**
在ViewTableViewCell.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
}
}
在ViewController.swift
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()
}
}
希望这个回答对您有所帮助。快乐编码:)
也许我的问题重复了。但是没有一个答案对我没有帮助。
现在我有 UITableViewController 和 static cells 并且每个 static cells 的 rowHeight 不同。
我有 UIButton 可以帮助我在 UILabel.
中显示全文- 第一行有 implement collectionView 和 height == 490.0
- 第二行在 UILabel 中有文本,我希望在单击 UIButton 时以全文显示,默认高度为 150.0 , 但如果文字会有很多文字,我需要更高的高度
- 第三行有实现collectionView和height == 150.0
- 第四行有implement collectionView and height == 150.0
- 第五行有UILabel和height == 50.0
还有我的屏幕截图我在说什么。
还有我的代码:
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.
如果您将 UILabel
上的 .amountOfLines
属性 设置为类似 5 的值,则会自动截断字符串以适合 5 行。然后当用户点击阅读更多按钮时,将其更改为 0 以允许 UILabel
有无限行来显示整个文本。如果您取消对单元格设置的高度限制并正确设置自动布局,它会自动缩放。
此外,如果您希望 UILabel 的展开具有动画效果,您可以在此处找到该解决方案:
@Дмитрий Деникаев。
我找到了解决方案。你可以查看我的工作演示项目here.。
1) 你需要设置UILabel
属性 setNumberOfLines = 0
.
2) 创建两个@IBOutlet
视图增加和减少约束并设置其priority
。前任。 priority = 750
和 priority = 250
(反之亦然)。
标签固定高度的第一个约束,它在故事板中的优先级是 750。
标签底部对其父视图的第二个约束,它在故事板中的优先级是 250。
**查看以下代码**
在ViewTableViewCell.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
}
}
在ViewController.swift
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()
}
}
希望这个回答对您有所帮助。快乐编码:)