问题
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.
 
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 ?
回答1:
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;
}
回答2:
To solve this problem you have to do two things
- set proper autoresizingMask to all your child views - headerView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin; middleView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth; footerView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
- return proper height from UITableViewDelegate method - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return [self heightForMiddleViewAtIndexPath: indexPath] + footerViewHeight + headerViewheight; } 
- (CGFloat)heightForMiddleViewAtIndexPath:(NSIndexPath *)indexPath { \ do some height calculation; } 
 
来源:https://stackoverflow.com/questions/15245488/expandable-and-collapsable-uitableviewcell-with-custom-views