Search code examples
iphoneuiviewuitableviewios6

Expandable and Collapsable UITableViewCell with Custom Views


Not Getting any Idea on how to solve this ....

Ok. This is my custom UITableViewCell with three different Child Views. That you can see with different colors.

enter image description here

Here , Height of my second Child View is dynamic and be calculated at run time. I am using -heightForRowAtIndexPath to set the height of UITableViewCell.

What I have tried :

I set the Outlets of Child Views and then in -heightForRowAtIndexPath , I used this Code...

middleView.frame = CGRectMake(0, 60.0, 750.0, size);
footerView.frame = CGRectMake(0, size + 60.0, 750.0, 50.0);

where, size = Dynamically Calculated Height of Middle View.

UPDATE :

I have also tried with static value of size like this ...

 middleView.frame = CGRectMake(0, 60.0, 750.0, 350.0);
 footerView.frame = CGRectMake(0, 350.0 + 60.0, 750.0, 50.0);

But No Luck at All....

Size is calculated by this : size = (elements * 30) + 50;

where , elements = Number of elements in the Array.

Problem :

Look, at the Screenshot.

Any Suggestions ?


Solution

  • Set your middle view and footer view frame in following method instead of heightForRowAtIndexPath:

    and keep your other method as given by indra Mohan above.

     - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath       *)indexPath
    {   
    NSString * cellIdentifier = @"TableViewCellIdentifier"  ;
    InAppTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
    cell.delegate = self;
    float size = [self heightForMiddleViewAtIndexPath: indexPath]
    cell.middleView.frame = CGRectMake(0, 60.0, 750.0, size);
    cell.footerView.frame = CGRectMake(0, size + 60.0, 750.0, 50.0);
    return cell;
    

    }