Expandable tableView in iphone

前端 未结 12 1732
离开以前
离开以前 2020-11-27 17:53

\"enter

I want to make this type of expandable/collapsible table vie

相关标签:
12条回答
  • 2020-11-27 18:33

    I have a little bit of a different approach to expandable table views - one that aligns with how these kinds of table views are generally built.

    There are headers and there are cells. Headers should be tappable, and then cells underneath the headers would show or hide. This can be achieved by adding a gesture recognizer to the header, and when tapped, you just remove all of the cells underneath that header (the section), and viceversa (add cells). Of course, you have to maintain state of which headers are "open" and which headers are "closed."

    This is nice for a couple of reasons:

    1. The job of headers and cells are separated which makes code cleaner.
    2. This method flows nicely with how table views are built (headers and cells) and, therefore, there isn't much magic - the code is simply removing or adding cells, and should be compatible with later versions of iOS.

    I made a very simple library to achieve this. As long as your table view is set up with UITableView section headers and cells, all you have to do is subclass the tableview and the header. Try it :)

    Link: https://github.com/fuzz-productions/FZAccordionTableView

    0 讨论(0)
  • 2020-11-27 18:38

    You may take a look at this accordion example in Swift: https://github.com/tadija/AEAccordion

    enter image description here

    It's got very little code to create accordion effect (not by using sections but cells), and as a bonus there is also a solution to use XIB files inside other XIB files (useful for custom cells which use custom views).

    0 讨论(0)
  • 2020-11-27 18:39

    Use Following code for expandable Cell into UITableView

    - (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] autorelease];
        }
        cell.textLabel.text=[[self.arForTable objectAtIndex:indexPath.row] valueForKey:@"name"];
        [cell setIndentationLevel:[[[self.arForTable objectAtIndex:indexPath.row] valueForKey:@"level"] intValue]];    
        return cell;
    }
    

    code for expanding & collapsing rows – TableView DidSelectRow Method

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
        NSDictionary *d=[self.arForTable objectAtIndex:indexPath.row];
        if([d valueForKey:@"Objects"]) {
            NSArray *ar=[d valueForKey:@"Objects"];
            BOOL isAlreadyInserted=NO;
            for(NSDictionary *dInner in ar ){
                NSInteger index=[self.arForTable indexOfObjectIdenticalTo:dInner];
                isAlreadyInserted=(index>0 && index!=NSIntegerMax);
                if(isAlreadyInserted) break; 
            }
            if(isAlreadyInserted) {
                [self miniMizeThisRows:ar];
            } else {        
                NSUInteger count=indexPath.row+1;
                NSMutableArray *arCells=[NSMutableArray array];
                for(NSDictionary *dInner in ar ) {
                    [arCells addObject:[NSIndexPath indexPathForRow:count inSection:0]];
                    [self.arForTable insertObject:dInner atIndex:count++];
                }
                [tableView insertRowsAtIndexPaths:arCells withRowAnimation:UITableViewRowAnimationLeft];
            }
        }
    }
    

    A Method which will help to minimize & maximize/expand-collapse rows.

    -(void)miniMizeThisRows:(NSArray*)ar{
        for(NSDictionary *dInner in ar ) {
            NSUInteger indexToRemove=[self.arForTable indexOfObjectIdenticalTo:dInner];        
            NSArray *arInner=[dInner valueForKey:@"Objects"];
            if(arInner && [arInner count]>0){
                [self miniMizeThisRows:arInner];
            }
            if([self.arForTable indexOfObjectIdenticalTo:dInner]!=NSNotFound) {
                [self.arForTable removeObjectIdenticalTo:dInner];
                [self.tableView deleteRowsAtIndexPaths:
                  [NSArray arrayWithObject:[NSIndexPath indexPathForRow:indexToRemove inSection:0]]
                          withRowAnimation:UITableViewRowAnimationRight];
            }
        }
    }
    

    You can download the source code from my tutorial site.

    0 讨论(0)
  • 2020-11-27 18:40

    There is a great video in WWDC 2011 called UITableView Changes, Tips and Tricks - session 125 that shows how to do things like this.
    Also check out the example code TVAnimationsGestures

    0 讨论(0)
  • 2020-11-27 18:46

    Please try this example :

    best example for Expandable TableView

    https://github.com/OliverLetterer/UIExpandableTableView

    0 讨论(0)
  • 2020-11-27 18:53

    Check this Link :

    http://iostechnotips.blogspot.in/2014/05/expandable-uitableview.html

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

    *Use UITableView delegate method viewForHeaderInSection and return a custom UIView.

    *Add a UIButton as subview with action "expandable:(id)sender" check the sender id as section number and reload the table view.

    0 讨论(0)
提交回复
热议问题