Expandable and Collapsable UITableViewCell with Custom Views

旧城冷巷雨未停 提交于 2020-01-05 07:41:52

问题


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

  1. set proper autoresizingMask to all your child views

        headerView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin;
    middleView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    footerView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin; 

  2. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!