Expand/collapse section in UITableView in iOS

前端 未结 17 925
野的像风
野的像风 2020-11-22 08:49

Could somebody tell me the way to perform UITableView expandable/collapsible animations in sections of UITableView as below?

<

17条回答
  •  轮回少年
    2020-11-22 09:50

    I ended up just creating a headerView that contained a button ( i saw Son Nguyen's solution above after the fact, but heres my code.. it looks like a lot but it's pretty simple):

    declare a couple bools for you sections

    bool customerIsCollapsed = NO;
    bool siteIsCollapsed = NO;
    

    ...code

    now in your tableview delegate methods...

    - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
    {
        UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, _tblSearchResults.frame.size.width, 35)];
    
        UILabel *lblSection = [UILabel new];
        [lblSection setFrame:CGRectMake(0, 0, 300, 30)];
        [lblSection setFont:[UIFont fontWithName:@"Helvetica-Bold" size:17]];
        [lblSection setBackgroundColor:[UIColor clearColor]];
        lblSection.alpha = 0.5;
        if(section == 0)
        {
            if(!customerIsCollapsed)
                [lblSection setText:@"Customers    --touch to show--"];
            else
                [lblSection setText:@"Customers    --touch to hide--"];
        }
        else
        {
            if(!siteIsCollapsed)
                [lblSection setText:@"Sites    --touch to show--"];
            else
                [lblSection setText:@"Sites    --touch to hide--"];    }
    
        UIButton *btnCollapse = [UIButton buttonWithType:UIButtonTypeCustom];
        [btnCollapse setFrame:CGRectMake(0, 0, _tblSearchResults.frame.size.width, 35)];
        [btnCollapse setBackgroundColor:[UIColor clearColor]];
        [btnCollapse addTarget:self action:@selector(touchedSection:) forControlEvents:UIControlEventTouchUpInside];
        btnCollapse.tag = section;
    
    
        [headerView addSubview:lblSection];
        [headerView addSubview:btnCollapse];
    
        return headerView;
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        // Return the number of rows in the section.
        if(section == 0)
        {
            if(customerIsCollapsed)
                return 0;
            else
                return _customerArray.count;
        }
        else if (section == 1)
        {
            if(siteIsCollapsed)
                return 0;
            else
            return _siteArray.count;
    
        }
        return 0;
    }
    

    and finally the function that gets called when you touch one of the section header buttons:

    - (IBAction)touchedSection:(id)sender
    {
        UIButton *btnSection = (UIButton *)sender;
    
        if(btnSection.tag == 0)
        {
            NSLog(@"Touched Customers header");
            if(!customerIsCollapsed)
                customerIsCollapsed = YES;
            else
                customerIsCollapsed = NO;
    
        }
        else if(btnSection.tag == 1)
        {
            NSLog(@"Touched Site header");
            if(!siteIsCollapsed)
                siteIsCollapsed = YES;
            else
                siteIsCollapsed = NO;
    
        }
        [_tblSearchResults reloadData];
    }
    

提交回复
热议问题