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!) {
}
}
First you have to keep track of the indexPath of currently selected cell in a property:
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):
Now you have to implement
tableView(_:, heightForRowAtIndexPath:)
:Now in your
tableView(_:, didSelectRowAtIndexPath:)
method you have to check wether the selected row or an unselected row has been tapped:The
beginUpdates()
andendUpdates()
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()
andendUpdates()
calls in an animation blockUIView.animationWithDuration(...)
and set it to whatever value you want.You can check out this sample project which demonstrates this code in action.