簡體   English   中英

點擊單元格后不同的 UITableViewCell 子視圖布局

[英]Different UITableViewCell subviews layout after tapping a cell

我創建了一個具有多個單元格的 UITableView。 所有單元格都相同,我想在點擊單元格后更改單元格的子視圖布局和單元格的高度。 更改單元格本身的高度非常容易,但我不知道如何更改單元格的子視圖布局......

我應該重新加載一個新的自定義單元格而不是點擊的單元格還是只更新單元格子視圖框架?

這是正常的布局:

埃爾

這是新的布局:

埃爾

約束不是問題,我的問題是如何添加在普通單元格中未使用的元素,現在需要重新構建和放大它們(例如主要的 UIImageView 和 UILabel 的)

我會有不同的單元格並在單擊單元格時重新加載它們。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM