【问题标题】:how to change the height of a single cell when clicked?单击时如何更改单个单元格的高度?
【发布时间】:2014-07-23 18:53:12
【问题描述】:

单击时,我必须调整 tableView 的单行大小。我怎么能做到这一点?有人可以帮帮我吗?

我的视图控制器类:

class DayViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

    @IBOutlet var daysWorkPointTable: UITableView

    override func viewDidLoad() {
        super.viewDidLoad()

        var nipName = UINib(nibName: "daysWorkPointsCell", bundle: nil)

        self.daysWorkPointTable.registerNib(nipName, forCellReuseIdentifier: "daysWorkCell")
    }

    func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {
        return 1
    }

    func tableView(tableView:UITableView!, heightForRowAtIndexPath indexPath:NSIndexPath) -> CGFloat {
        return 75
    }

    func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
        var cell = tableView.dequeueReusableCellWithIdentifier("daysWorkCell", forIndexPath: indexPath) as daysWorkPointsCell

        return cell
    }

    func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {

    }
}

【问题讨论】:

    标签: ios uitableview swift ios8 xcode6


    【解决方案1】:

    首先,您必须在属性中跟踪当前选定单元格的 indexPath:

    var selectedCellIndexPath: NSIndexPath?
    

    它应该是可选的,因为您不能选择任何单元格。 接下来让我们声明选中和未选中状态的高度(将值更改为您想要的任何值):

    let selectedCellHeight: CGFloat = 88.0
    let unselectedCellHeight: CGFloat = 44.0
    

    现在你必须实现tableView(_:, heightForRowAtIndexPath:)

    override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        if selectedCellIndexPath == indexPath {
            return selectedCellHeight
        }
        return unselectedCellHeight
    }
    

    现在在您的tableView(_:, didSelectRowAtIndexPath:) 方法中,您必须检查是否已点击了选定行或未选定行:

    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        if selectedCellIndexPath != nil && selectedCellIndexPath == indexPath {
            selectedCellIndexPath = nil
        } else {
            selectedCellIndexPath = indexPath
        }
    
        tableView.beginUpdates()
        tableView.endUpdates()
    
        if selectedCellIndexPath != nil {
            // This ensures, that the cell is fully visible once expanded
            tableView.scrollToRowAtIndexPath(indexPath, atScrollPosition: .None, animated: true)
        }
    }
    

    beginUpdates()endUpdates() 调用为您提供动画高度变化。

    如果您想更改高度变化动画的持续时间,您可以将 beginUpdates()endUpdates() 调用包装在动画块 UIView.animationWithDuration(...) 中,并将其设置为您想要的任何值。

    您可以查看this sample project,它演示了此代码的实际作用。

    【讨论】:

    • Argh,只是读到你希望它只在一个单元格上工作。我的实现有点矫枉过正,因为它适用于任意数量的单元格。
    • 如果声明为 let,我无法在验证中为 selectedCellIndexPath 分配值,因此我尝试将 let 更改为 var,但继续出错,因为我无法将 nil 分配给一个 NSIndexPath。你能帮助我吗?我做错了什么?
    • 我将分配更改为显式分配给 self.selectedCellIndexPath。它现在应该可以工作了,希望 ;)。
    • 使用 beginUpdates() 和 endUpdates 后,scrollToRowAtIndexPath 工作正常。但是 tableView.setContentOffset(_, animated:) 在调用这 2 个方法后不起作用!任何想法为什么?看起来像一个错误(((
    • 自 2020 年 9 月起,beginUpdates/endUpdates 不仅为您提供动画,而且是其工作所必需的
    猜你喜欢
    • 1970-01-01
    • 2021-10-28
    • 1970-01-01
    • 1970-01-01
    • 2020-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多