0

I have made a expandable and collapsable tableview .Where if sections are tapped they expand and if they are already expanded they collapse.

For section thing,I have taken a view .Inside the UIView, there is label and on label there is tap gesture.It is working fine. Now, the problem is that the size of label and view is fix, but actually I want the label to expand if the text inside the label is more than the height of the label.But , I ma no able to achieve it even several efforts.Kindly check the following code and give some directions to move further.

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{

    UIView *sectionView=[[UIView alloc]initWithFrame:CGRectMake(30, 5,self.expandableTableView.frame.size.width-30,40)];
    sectionView.tag=section;
    UILabel *viewLabel=[[UILabel alloc]initWithFrame:CGRectMake(10,5,sectionView.frame.size.width-60, 40)];
    imgView=[[UIImageView alloc]initWithFrame:CGRectMake(viewLabel.frame.size.width+10,10,30, 30)];
    imgView.image = [UIImage imageNamed:@"down.png"];
    viewLabel.backgroundColor=[UIColor clearColor];
    viewLabel.textColor=[UIColor darkTextColor];
    viewLabel.font=[UIFont systemFontOfSize:15];
    viewLabel.text=[NSString stringWithFormat:@"List of %@",[_sectionArray objectAtIndex:section]];
    [sectionView addSubview:viewLabel];
    [sectionView addSubview:imgView];
    /********** Add a border with Section view *******************/

    sectionView.layer.borderWidth = 1;
    sectionView.layer.borderColor =  [[UIColor grayColor] CGColor];
    sectionView.layer.cornerRadius = 5;


    /********** Add UITapGestureRecognizer to SectionView   **************/

    UITapGestureRecognizer  *headerTapped   = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(sectionHeaderTapped:)];
    [sectionView addGestureRecognizer:headerTapped];

    return  sectionView;


}

Any help would be appreciated .Thanks in advance

Prez
  • 227
  • 3
  • 12
  • Did you take a look at this question https://stackoverflow.com/questions/25642493/dynamic-uicollectionview-header-size-based-on-uilabel? – trungduc Nov 25 '17 at 10:53

1 Answers1

0

Your answer for - "but actually I want the label to expand if the text inside the label is more than the height of the label"

[viewLabel sizeToFit]

actually you are trying to return a view. Designing such kind of view in storyboard using auto layout is much more easier.

For collapsing or expanding a tableview, you can do another way. you can set this label in a table cell. Then you can dynamically create cells, insert the cells in an array, reload the tableview using the array. sets their height using

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;

when you call didDeselectRowAtIndexPath , the cell will be deselected. You can return the height 0 using heightForRowAtIndexPath.

I hope this is what you are searching.

Ankur Lahiry
  • 2,253
  • 1
  • 15
  • 25