【问题标题】:Issue in increase height of cell on Read more UIButton阅读更多 UIButton 上的单元格高度增加问题
【发布时间】:2019-03-31 19:56:45
【问题描述】:

也许我的问题会重复。但是没有一个答案对我没有帮助。

现在我有了 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.

【问题讨论】:

    标签: ios swift xcode uitableview nslayoutconstraint


    【解决方案1】:

    @Дмитрий Деникаев。

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

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

    2) 为视图增加和减少创建两个@IBOutlet 约束并设置其priority。前任。 priority = 750priority = 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()
    
        }
    }
    

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

    【讨论】:

    • 这个安装了,不明白如何配置cell的部署
    • 它适用于静态单元格吗?因为我的 tableView 有静态单元格
    • 是的,它在静态单元格和动态单元格中都有效。只是你必须设置你的条件
    • 如果您有任何问题,请告诉我。谢谢
    • 谢谢,很好的解决方案!它帮助了我。
    【解决方案2】:

    如果您将UILabel 上的.amountOfLines 属性设置为5,则会自动截断字符串以适合5 行。然后当用户点击 Read More 按钮时,将其更改为 0 以允许 UILabel 有无限行来显示整个文本。如果您删除了对单元格的高度限制并正确设置了自动布局,它将自动缩放。

    另外,如果你想让 UILabel 的扩展动画化,你可以在这里找到解决方案:https://stackoverflow.com/a/34284563/5544222

    【讨论】:

      猜你喜欢
      • 2017-06-23
      • 1970-01-01
      • 2017-04-26
      • 2018-09-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-22
      相关资源
      最近更新 更多