Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Change UITableViewCell's Height on Tap?

I'm trying to have a UITableViewCell reveal more content about a particular cell whenever that specific cell is tapped. Once the cell has expanded and is tapped again, the cell should shrink back to its original size.

I'm pretty sure something has to be done with the delegates heightForRowAtIndexPath and didSelectRowAtIndexPath, but I don't know how to select a specific table cell row using didSelectRowAtIndexPath.

// Height of table cell rows
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return 45
}

//On cell tap, expand
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    self.tableView.rowHeight = 75;
}

Also, is it possible to hide any content that's overflowing from the parent cell? Kind of like overflow: hidden; in CSS.

like image 862
10000RubyPools Avatar asked Nov 27 '22 05:11

10000RubyPools


1 Answers

Declare a global variable NSInteger type and store row on tableview selected in didSelectRowAtIndexPath and also reload here . After check row in heightForRowAtIndexPath and increase height there . Try Like this

var selectedIndex : NSInteger! = -1 //Delecre this global 

 func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    if indexPath.row == selectedIndex{
        selectedIndex = -1
    }else{
        selectedIndex = indexPath.row
    }
    tableView.reloadData()
}

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    if indexPath.row == selectedIndex
    {
        return 75
    }else{
        return 45
    }
}
like image 79
Jogendra.Com Avatar answered Jan 19 '23 01:01

Jogendra.Com