Expandable tableView in iphone

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

\"enter

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

12条回答
  •  我在风中等你
    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;
    }
    

提交回复
热议问题