I have a UITableView with a custom cell. There is a Button, one label and one hidden label in the cell. I want the hidden label be visible after clicking the button. But when I am using self sizing cells I can't reload my tableView after setting the hidden label to visible.
The self sizing cells are working just fine with these two lines of code in the viewDidLoad() function.
self.tableView.estimatedRowHeight = 68.0
self.tableView.rowHeight = UITableViewAutomaticDimension
This is my ViewController class:
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
//Self sizing cells
self.tableView.estimatedRowHeight = 68.0
self.tableView.rowHeight = UITableViewAutomaticDimension
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! CustomCell
cell.leftLabel.text = "Left Label"
cell.centerLabel.text = "I am the center label and I need a little more words because I am supposed to be a wrap content. I hope this is enough"
cell.button.tag = indexPath.row
cell.button.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside)
return cell
}
func buttonAction(sender: AnyObject) {
var button: UIButton = sender as! UIButton
let indexPath: NSIndexPath = NSIndexPath(forRow: button.tag, inSection: 0)
var cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! CustomCell
cell.centerLabel.hidden = false
self.tableView.reloadData()
}
}
As soon as I call
self.tableView.estimatedRowHeight = 68.0
the reloadData call in the buttonAction function doesn't work anymore.
Can anybody help me out with this problem? Thanks!
After reloadData, your cell.centerLabel.hidden is also invalidated. As you dont't change the centerLable.hidden in cellForXXX, the centerLable-hidden cell will be at anywhere in your table.
Try this. You use cellForRowAtIndexPath to grab the cell at that position in the tableView, not dequeueReusableCellWithIdentifier
func buttonAction(sender: UIButton) {
var indexPath:NSIndexPath = NSIndexPath(forRow:sender.tag, inSection: 0)
var cell = tableView.cellForRowAtIndexPath(indexPath) as! CustomCell
cell.centerLabel.hidden = false
}
Then if you want to hide the centerLabel when your cells are reused, add this method in your CustomCell class
override func prepareForReuse() {
self.centerLabel.hidden = true
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.