增加细胞高度的问题阅读更多UIButton

问题描述 投票:1回答:2

也许我的问题重演了。但没有一个答案没有帮助mea。

现在我在每个静态单元格中都有UITableViewController和静态单元格以及不同的rowHeight。

我有UIButton谁应该帮我透露我的UILabel中的全文。

  • 第一行实现了collectionView和height == 490.0
  • 第二行在UILabel中有文本,当我点击UIButton和高度默认为150.0时,我希望以全文显示,但如果文本有很多文本,我需要更多高度
  • 第三行实现了collectionView和height == 150.0
  • 第四行实现了collectionView和height == 150.0
  • 第五行有UILabel,高度== 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.

enter image description here

ios swift xcode uitableview nslayoutconstraint
2个回答
1
投票

@Dmitry Denikayev。

我找到了解决方案。你可以查看我的工作演示项目here.

1)你需要设置UILabel属性setNumberOfLines = 0

enter image description here

2)为视图增加和减少创建两个@IBOutlet约束并设置其priority。恩。 priority = 750priority = 250(反之亦然)。

  • 标签修复高度的第一个约束及其在故事板中的优先级为750。 qazxsw poi
  • 第二个限制标签底部到其超级视图,它在故事板中的优先级是250。 enter image description here

**查看以下代码**

在ViewTableViewCell.swift中

enter image description here

在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
    }

}

我希望这个答案对你有帮助。快乐编码:)


0
投票

如果你将 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

© www.soinside.com 2019 - 2024. All rights reserved.