点击单元格后不同的 UITableViewCell 子视图布局

Posted

技术标签:

【中文标题】点击单元格后不同的 UITableViewCell 子视图布局【英文标题】:Different UITableViewCell subviews layout after tapping a cell 【发布时间】:2020-04-16 16:41:22 【问题描述】:

我创建了一个包含多个单元格的 UITableView。所有单元格都相同,我想在点击单元格后更改单元格的子视图布局和单元格的高度。更改单元格本身的高度很容易,但我不知道如何更改单元格的子视图布局...

我应该重新加载一个新的自定义单元格而不是点击的单元格还是只更新单元格子视图框架?

这是正常的布局:

这是新的布局:

约束不是问题,我的问题是如何添加在普通单元格中未使用的元素,现在它们需要重新构图和放大(例如主 UIImageView 和 UILabel 的)

【问题讨论】:

我会使用不同的单元格。跟踪哪些单元格被点击并致电reloadRows(at:with:) 所以我需要创建一个boolean,比如说resized,并根据用户手势设置为true/false。然后重新加载didSelectRowAt中的特定单元格? 可以调整多个单元格的大小吗?换句话说,如果您单击一个单元格。然后点击另一个单元格,两者都会被重新定义吗? @Rob,对不起,我不明白你的问题。我不需要调整多个单元格的大小。一次只有一个。 两种状态之间的过渡动​​画对你来说重要吗? 【参考方案1】:

我会有不同的单元格并在单击单元格时重新加载它们。

class TableViewController: UITableViewController 

    var largeRow: IndexPath?

    var data: [Int] = [0, 1, 2, 3, 4, 5, 6, 7]

    override func viewDidLoad() 
        super.viewDidLoad()
    


extension TableViewController 

    override func numberOfSections(in tableView: UITableView) -> Int 
        return 1
    

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int 
        return data.count
    

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 
        if indexPath == largeRow 
            return makeLargeCell(at: indexPath)
         else 
            return makeNormalCell(at: indexPath)
        
    


extension TableViewController 
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) 
        tableView.deselectRow(at: indexPath, animated: true)

        if let largeRow = largeRow 
            if largeRow == indexPath 
                self.largeRow = nil
                tableView.reloadRows(at: [indexPath], with: .fade)
             else 
                self.largeRow = indexPath
                tableView.reloadRows(at: [largeRow, indexPath], with: .fade)
            
         else 
            self.largeRow = indexPath
            tableView.reloadRows(at: [indexPath], with: .fade)
        
    

【讨论】:

以上是关于点击单元格后不同的 UITableViewCell 子视图布局的主要内容,如果未能解决你的问题,请参考以下文章

iOS 在点击时使用 AutoLayout 展开 UITableViewCell

在前 5 个单元格后未调用 didSelectRowAtIndexPath

在点击不可选择的单元格后取消选择以前选择的 UICollectionViewCells

UITableViewCell 和 UITextView 之间的 UIMenuController 奇怪行为

将值从 UITableViewCell 传递到详细视图

UITableViewCell 只在滚动后显示附件