Expand/collapse section in UITableView in iOS

前端 未结 17 921
野的像风
野的像风 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:36

    This is the best way i found to create expandable table view cells

    .h file

      NSMutableIndexSet *expandedSections;
    

    .m file

    if (!expandedSections)
        {
            expandedSections = [[NSMutableIndexSet alloc] init];
        }
       UITableView *masterTable = [[UITableView alloc] initWithFrame:CGRectMake(0,100,1024,648) style:UITableViewStyleGrouped];
        masterTable.delegate = self;
        masterTable.dataSource = self;
        [self.view addSubview:masterTable];
    

    Table view delegate methods

    - (BOOL)tableView:(UITableView *)tableView canCollapseSection:(NSInteger)section
    {
        // if (section>0) return YES;
    
        return YES;
    }
    
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
        // Return the number of sections.
        return 4;
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        if ([self tableView:tableView canCollapseSection:section])
        {
            if ([expandedSections containsIndex:section])
            {
                return 5; // return rows when expanded
            }
    
            return 1; // only top row showing
        }
    
        // Return the number of rows in the section.
        return 1;
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"Cell";
    
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ;
        }
    
        // Configure the cell...
    
        if ([self tableView:tableView canCollapseSection:indexPath.section])
        {
            if (!indexPath.row)
            {
                // first row
                cell.textLabel.text = @"Expandable"; // only top row showing
    
                if ([expandedSections containsIndex:indexPath.section])
                {
    
                    UIImageView *imView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"UITableContract"]];
                    cell.accessoryView = imView;
                }
                else
                {
    
                    UIImageView *imView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"UITableExpand"]];
                    cell.accessoryView = imView;
                }
            }
            else
            {
                // all other rows
                if (indexPath.section == 0) {
                    cell.textLabel.text = @"section one";
                }else if (indexPath.section == 1) {
                    cell.textLabel.text = @"section 2";
                }else if (indexPath.section == 2) {
                    cell.textLabel.text = @"3";
                }else {
                    cell.textLabel.text = @"some other sections";
                }
    
                cell.accessoryView = nil;
                cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            }
        }
        else
        {
            cell.accessoryView = nil;
            cell.textLabel.text = @"Normal Cell";
    
        }
    
        return cell;
    }
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        if ([self tableView:tableView canCollapseSection:indexPath.section])
        {
            if (!indexPath.row)
            {
                // only first row toggles exapand/collapse
                [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
                NSInteger section = indexPath.section;
                BOOL currentlyExpanded = [expandedSections containsIndex:section];
                NSInteger rows;
    
    
                NSMutableArray *tmpArray = [NSMutableArray array];
    
                if (currentlyExpanded)
                {
                    rows = [self tableView:tableView numberOfRowsInSection:section];
                    [expandedSections removeIndex:section];
    
                }
                else
                {
                    [expandedSections addIndex:section];
                    rows = [self tableView:tableView numberOfRowsInSection:section];
                }
    
    
                for (int i=1; i

提交回复
热议问题