how to change the height of a single cell when clicked?

孤街浪徒 提交于 2019-12-29 03:33:09

问题


I have to resize a single row of my tableView when clicked. How I can do this? Anybody could help me?

My view controller class:

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!) {

    }
}

回答1:


First you have to keep track of the indexPath of currently selected cell in a property:

var selectedCellIndexPath: NSIndexPath?

It should be an optional, because you can have no cell selected. Next lets declare heights for selected and unselected state (change the values to whatever you want):

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

Now you have to implement tableView(_:, heightForRowAtIndexPath:):

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

Now in your tableView(_:, didSelectRowAtIndexPath:) method you have to check wether the selected row or an unselected row has been tapped:

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

The beginUpdates() and endUpdates() calls are giving you an animated height change.

If you want to change the duration of the height change animation you can wrap the beginUpdates() and endUpdates() calls in an animation block UIView.animationWithDuration(...) and set it to whatever value you want.

You can check out this sample project which demonstrates this code in action.



来源:https://stackoverflow.com/questions/24918598/how-to-change-the-height-of-a-single-cell-when-clicked

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!