Swift 3 - 条件扩展表格视图单元格高度

时间:2017-03-23 13:57:30

标签: ios swift swift3

我正在使用Swift 3.我有可扩展的表格视图单元格,但根据单击的单元格,是否可以获得不同的行高?例如,如果单击第一个单元格,我希望它返回420表示高度,如果单击其他单元格,我希望它返回300表示高度。

这是我的手机课。

class ResultsCell: UITableViewCell {

    @IBOutlet weak var introPara : UITextView!
    @IBOutlet weak var section_heading : UILabel!

    class var expandedHeight : CGFloat = { get { return 420.0 } }
    class var defaultHeight : CGFloat { get { return 44.0 } }

    var frameAdded = false

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    override func awakeFromNib() {
        super.awakeFromNib()
        section_heading.translatesAutoresizingMaskIntoConstraints = false

    }

    func checkHeight() {
        introPara.isHidden = (frame.size.height < ResultsCell.expandedHeight)
    }

    func watchFrameChanges() {
        if(!frameAdded) {
            addObserver(self, forKeyPath: "frame", options: .new, context: nil)
            checkHeight()
        }
    }

    func ignoreFrameChanges() {
        if(frameAdded){
            removeObserver(self, forKeyPath: "frame")
        }
    }

    deinit {
        print("deinit called");
        ignoreFrameChanges()
    }

    // when our frame changes, check if the frame height is appropriate and make it smaller or bigger depending
    override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
        if keyPath == "frame" {
            checkHeight()
        }
    }

}

我所尝试的是这样的。

var _expandedHeight : CGFloat = 420.0
class var expandedHeight : CGFloat { get { return _expandedHeight } set (newHeight) { _expandedHeight = newHeight } }
var isRow0 = false

override func awakeFromNib() {
    super.awakeFromNib()
    section_heading.translatesAutoresizingMaskIntoConstraints = false

    if isRow0 {
        ResultsCell.expandedHeight = 300.0
    }
    else {
        ResultsCell.expandedHeight = 420.0
    }
}

然后在TableViewController ...

// return the actual view for the cell
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let resultcell = tableView.dequeueReusableCell(withIdentifier: "resultCellTemplate", for: indexPath) as! ResultsCell

    if indexPath.row == 0 {
        resultcell.isRow0 = true
    } else {
        resultcell.isRow0 = false
    }

    return resultcell
}

但我在getter / setter行遇到错误:instance member _expandedHeight cannot be used on type ResultsCell

如何实现我想要的行为?

2 个答案:

答案 0 :(得分:0)

使用tableview更新方法重新评估每个单元格的高度

self.tableView.beginUpdates()
self.tableView.endUpdates()

使用heightForRowAtIndexPath自动计算对tableview单元格高度的更改 尝试设置你的身高w.r.t索引路径 让用户提到heightForRowAtIndexPath来计算正确的高度。

答案 1 :(得分:0)

您应该在TableView的ar中执行此操作。

您的TableView有一个delegate私有实例变量,其值应实现协议delegate。该协议包含许多UITableViewDelegate调用的函数,以便设置自己的外观。

欲了解更多信息:

UITableView协议: https://developer.apple.com/reference/uikit/uitableviewdelegate

UITableViewDelegate UITableView实例变量: https://developer.apple.com/reference/uikit/uitableview#delegate

您要做的是在delegate对象上实施tableView(_:heightForRowAt:)方法。此方法有两个参数:第一个是UITableViewDelegate本身,第二个是准备渲染的行的UITableViewIndexPath是一个对象,其对应于正在呈现的单元格具有IndexPathsection属性。

您要做的是根据row计算单元格是否展开的某种方式。

Swift IndexPath参考:https://developer.apple.com/reference/foundation/indexpath

尝试基于IndexPath本身执行此操作是一种不正确的方法,因为您通常不应在呈现单元格之前对其进行分析。

执行此操作的方法是让您的UITableViewCell - 继承类保留一个(可能是嵌套的)布尔数组,指示应该扩展哪些单元格。然后,您可以使用UITableView的{​​{1}}和可能IndexPath作为此数组的索引,以确定是否扩展了单元格。像这样:

row

设置数据的正确方法是调用section上的方法,但为了简单起见,我假设您已经在TableView上设置了数据。

请勿尝试手动设置class MyTableView: UITableView, UITableViewDelegate { /* ... */ private var expanded: [Bool] = [] init() { super.init() self.delegate = self } func setData(data: [SomeType]) { //set the data for the table here expanded = [Bool](repeating: false, count: data.count) } /* ...logic to expand and collapse cells... */ func tableView(_ view: UITableView, heightForRowAt indexPath: IndexPath) { if expanded[indexPath.row] { return expandedHeight } else { return collapsedHeight } } /* ... */ } 的属性。设置UITableViewDataSource的基于UITableViewdelegate的工作流程,以便您的表格视图有效运行,并且只在需要时自行更新。

相关问题