Expandable tableView in iphone

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

\"enter

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

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

    Try Using this code... May be this can help.. And Feel free to Edit the code according to your requirements...

    #import "ViewController.h"
    #import <QuartzCore/QuartzCore.h>
    @interface ViewController ()
    
    @end
    
    @implementation ViewController
    @synthesize myTable;
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        //myTable.backgroundColor=[UIColor clearColor];
       // self.view.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"wood.png"]];
        muArr= [[NSMutableArray alloc]initWithObjects:@"Vinay",@"Anmol",@"Jagriti", nil];
        ExpArr=[[NSMutableArray alloc]initWithObjects:@"Useeee",@"Thissss",@"Codeee", nil];
        otherExpand=100;
        checker=100;
    
    }
    
    -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
    {
        return muArr.count;
    }
    
    -(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        if(otherExpand==section)
        return ExpArr.count;
    
        return  0;
    
    }
    
    -(BOOL)tableView:(UITableView *)table canCollapse:(NSIndexPath *)indexPath
    {
    
        return NO;
    }
    
    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *Identifier=@"Cell";
        UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:Identifier];
        if (cell==nil)
        {
            cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Identifier];
    
        }
        cell.textLabel.text=[ExpArr objectAtIndex:indexPath.row];
        cell.textLabel.backgroundColor=[UIColor clearColor];
        UIView *viewww=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
        viewww.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"wood.png"]];
        cell.backgroundView=viewww;
    
       // cell.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"wood.png"]];
        [tableView setSeparatorStyle:UITableViewCellSeparatorStyleSingleLineEtched];
        [tableView setSeparatorColor:[UIColor purpleColor]];
    
        return cell;
    
    }
    
    -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
    {
    
        UIView *view1=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
        [view1.layer setCornerRadius:20];
        view1.layer.borderWidth=2;
        view1.layer.borderColor=[UIColor brownColor].CGColor;
        UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(10, 0, 295, 44)];
        label.backgroundColor=[UIColor clearColor];
    
        label.text=[muArr objectAtIndex:section];
        UIButton *btn=[UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        btn.frame=CGRectMake(280, -5, 50, 50);
        btn.backgroundColor=[UIColor clearColor];
        btn.tag=section;
    
        view1.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"wood.png"]];
        label.textColor=[UIColor blackColor];
        label.font=[UIFont fontWithName:@"American TypeWriter" size:18];
        //btn.backgroundColor=[UIColor blackColor];
        [view1 addSubview:btn];
        [view1 addSubview:label];
    
        [btn addTarget:self action:@selector(Btntap:) forControlEvents:UIControlEventTouchUpInside];
    
        return view1;
    }
    
    
    -(void)Btntap : (UIButton *)btn
    {
        if(otherExpand!=100)
        {
            if (otherExpand==btn.tag)
            {
                NSMutableArray *tempArr2=[[NSMutableArray alloc]init];
                for(int j=0;j<ExpArr.count;j++)
                {
                    NSIndexPath *indexx1=[NSIndexPath indexPathForRow:j inSection:otherExpand];
                    [tempArr2 addObject:indexx1];
                }
                checker=0;
                otherExpand=100;
                [myTable deleteRowsAtIndexPaths:tempArr2 withRowAnimation:UITableViewRowAnimationAutomatic];
            }
    
            else
            {
                NSMutableArray *tempArr2=[[NSMutableArray alloc]init];
                for(int j=0;j<ExpArr.count;j++)
                {
                    NSIndexPath *indexx1=[NSIndexPath indexPathForRow:j inSection:otherExpand];
                    [tempArr2 addObject:indexx1];
                }
                checker=1;
                otherExpand=100;
                [myTable deleteRowsAtIndexPaths:tempArr2 withRowAnimation:UITableViewRowAnimationAutomatic];
            }
        }
    
        if(checker!=0)
        {
            otherExpand=btn.tag;
            //checker=
            NSMutableArray *tempArr=[[NSMutableArray alloc]init];
            for(int i=0;i<ExpArr.count;i++)
            {
                NSIndexPath *indexx=[NSIndexPath indexPathForRow:i inSection:btn.tag];
                [tempArr addObject:indexx];
            }
            [myTable insertRowsAtIndexPaths:tempArr withRowAnimation:UITableViewRowAnimationAutomatic];
    
            checker=1;
        }
        checker=100;
    }
    
    
    
    -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
    {
        return 44;
    }
    
    @end
    
    0 讨论(0)
  • 2020-11-27 18:30

    Finally i get two very useful helping link below which describes exact what the requirement is here Expanding/Collapsing TableView Sections
    Collapsable Table View for iOS

    Really, good articles for such kind of expanding/collapsing tableview sections

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

    You can use ExpyTableView

    Which makes an expandable section from your given cell. Compatible down to iOS 8.0. You will have flexibility by generating an expandable table view with multiple table view cells. Just manipulate the separators for states and then no one will know you are using multiple cells for expanding.

    • Other solutions: You manipulate the height to expand a cell, when an update needed in design of the cell, you have to re-construct all the auto-layout constraints or logic in code.

    • ExpyTableView: You make an expandable table view by using multiple cells and inserting and deleting them(which can mean expanding and collapsing), you will have a great chance in future design requests. All you will have to do is adding a new UITableViewCell and writing the code for it. You will easily have the new design.

    All you have to do is to import ExpyTableView and then:

    class ViewController: ExpyTableViewDataSource, ExpyTableViewDelegate {
    
      @IBOutlet weak var expandableTableView: ExpyTableView!
    
      // First, set data source and delegate for your table view.
      override func viewDidLoad() {
        super.viewDidLoad() 
        expandableTableView.dataSource = self
        expandableTableView.delegate = self
      }
    
      // Then return your expandable cell instance from expandingCell data source method.
      func expandableCell(forSection section: Int, inTableView tableView: ExpyTableView) -> UITableViewCell {
        // this cell will be displayed at IndexPath with section: section and row 0
      }
    } 
    

    You can see your former table view section is now an expandable table view section. You can also download the example project and see more detailed examples.

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

    TLIndexPathTools can do this sort of thing naturally. In fact, there is are extensions for both expandable sections and expandable tree structures. Try running the Collapse sample project for expandable sections and the Outline sample project for expandable trees.

    One advantage of using TLIndexPathTools is that, as a simple, low-level API, it can solve all kinds of dynamic table view and collection view problems using a common approach. And it works interchangeably with Core Data and plain arrays.

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

    If this helps: [Access uitableview's expandable and collapsable sections] https://github.com/OliverLetterer/UIExpandableTableView

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

    In your .h file

    LoadCustomCell *cell1;
    NSMutableArray  *arrayForBool;
    NSMutableArray  *questionArray;
    NSMutableArray  *answerArray;
    

    In your .m file

     viewDidLoadMethod {
     _faqTblView.estimatedRowHeight = 30;
     _faqTblView.rowHeight = UITableViewAutomaticDimension;
     arrayForBool = [[NSMutableArray alloc]init];
    
     _questionArray = [[NSMutableArray alloc]init];
     _answerArray = [[NSMutableArray alloc]init];
     for (int i = 0; i < _questionArray.count; i++) {
            [arrayForBool addObject:@"0"];
        }
        self.faqTblView.dataSource = self;
        self.faqTblView .delegate = self;
        [self.faqTblView reloadData];
     }
    

    after that

     #pragma mark - TableView Datasource & Delegate Method.
    -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
      return [_questionArray count];
     }
    - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
        UILabel *lblText = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 260, 100)];
        lblText.text = [_questionArray objectAtIndex:section];
        return [lblText getLabelHeight] + 20;(created custom class)
     }
    -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    UITapGestureRecognizer  *headerTapped   = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(sectionHeaderTapped:)];
    cell1 = [[[NSBundle mainBundle] loadNibNamed:@"LoadCustomCell" owner:self options:nil] objectAtIndex:0];
    [cell1 setFrame:CGRectMake(0, 0, cell1.frame.size.width, cell1.frame.size.height)];
    NSString *numStr = [NSString stringWithFormat:@"%ld. ",section + 1];
    [cell1.sideMenuUserNameLabel setText:[numStr stringByAppendingString:[_questionArray objectAtIndex:section]]];
    [cell1 setBackgroundColor:[UIColor lightGrayColor]];
    cell1.tag = section;
    [cell1 addGestureRecognizer:headerTapped];
    
    return cell1;
    }
    
    - (void)sectionHeaderTapped:(UITapGestureRecognizer *)gestureRecognizer {
    
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:gestureRecognizer.view.tag];
    if (indexPath.row == 0) {
        BOOL collapsed  = [[arrayForBool objectAtIndex:indexPath.section] boolValue];
        for (int i = 0; i < [_questionArray count]; i++) {
            if (indexPath.section==i) {
                [arrayForBool removeObjectAtIndex:i];
                [arrayForBool insertObject:[NSString stringWithFormat:@"%d", !collapsed] atIndex:i];
            }
        }
        NSLog(@"%@", arrayForBool);
        [self.faqTblView reloadSections:[NSIndexSet indexSetWithIndex:gestureRecognizer.view.tag] withRowAnimation:UITableViewRowAnimationAutomatic];
        for (NSIndexPath *indexPath in self.faqTblView.indexPathsForSelectedRows) {
            [self.faqTblView deselectRowAtIndexPath:indexPath animated:NO];
             }
        cell1.imageView.transform = CGAffineTransformMakeRotation(M_PI);
         }
    }
    
    -(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *questionCellIdentifier = @"questionCellIdentifier";
    QuestionCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:questionCellIdentifier];
    if (cell == nil) {
        NSArray * myNib;
        myNib =[[NSBundle mainBundle]loadNibNamed:@"QuestionCustomCell" owner:self options:nil];
        cell = (QuestionCustomCell *)[myNib lastObject];
    }
    
    BOOL manyCells  = [[arrayForBool objectAtIndex:indexPath.section] boolValue];
    if(manyCells){
        cell.questionNameLbl.text = [_answerArray objectAtIndex:indexPath.section];
    }
    return cell;
    }
    
    0 讨论(0)
提交回复
热议问题