How to expand and collapse rows using header section in a UITableView

前端 未结 4 1640
心在旅途
心在旅途 2020-12-14 14:08

I have a table view. Now I want to collapse and expand rows by tapping on the section header. In other words, when I tap the header the rows display for that section. How ca

4条回答
  •  悲哀的现实
    2020-12-14 14:21

    configure viewForHeaderInSection:

    like this

    (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    
        UILabel *lblHeader = [[UILabel alloc]init];
    
            lblHeader.text = @"Section 0";        
    
        lblHeader.backgroundColor = [UIColor blueColor];
        lblHeader.font = [UIFont fontWithName:@"Avenir" size:18];
        lblHeader.textAlignment=NSTextAlignmentLeft;
        lblHeader.userInteractionEnabled=YES;
        UIGestureRecognizer *gr;
    if(section==0){
        lblHeader.text = @"Section 0";  
        gr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];
    }else if(section == 1){
        lblHeader.text = @"Section 1";  
        gr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture1:)];
    }
    
    
    
        [lblHeader addGestureRecognizer:gr];
        return lblHeader;
    }
    

    then write seperate action calls

    - (void)handleGesture:(UIGestureRecognizer *)gestureRecognizer { 
    }
    
    - (void)handleGesture1:(UIGestureRecognizer *)gestureRecognizer { 
    }
    

提交回复
热议问题