单击时如何更改单个单元格的高度?

时间:2022-03-14 18:56:05

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

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

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 个解决方案

#1


33  

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

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

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

现在你必须实现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:

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

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

beginUpdates()和endUpdates()调用为您提供动画高度更改。

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.

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

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

您可以查看此示例项目,该项目演示了此代码的实际运行情况。

#1


33  

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

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

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

现在你必须实现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:

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

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

beginUpdates()和endUpdates()调用为您提供动画高度更改。

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.

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

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

您可以查看此示例项目,该项目演示了此代码的实际运行情况。