UItableViewのCell効果を表示/折りたたむには

2314 ワード

アイデア:最初はtableViewのすべてのパーティションヘッダが表示され、デフォルトでは各パーティションに0行があり、パーティションヘッダをクリックし、そのパーティションにデータを挿入しreloadData
  • (1)カスタムDataModelクラスを定義し、cellのクリック状態
  • を記録する
    @interface DataModel : NSObject
     //       
    @property (nonatomic) NSString *sectionTitle;
    //           fold   
    @property (nonatomic) BOOL fold;
    //section    @property (nonatomic) NSArray *rowDataArr;
    @end
    
  • (2)カスタムTableViewHeaderFooterView追加拡張機能:
  • @interface SectionHeaderView : UITableViewHeaderFooterView
    @property (nonatomic) UIButton *btn;
    @property (nonatomic) NSInteger section;
    @property (nonatomic, copy) void(^btnClicked)(NSInteger section);
    @end
    

    方法実装:@implementation SectionHeaderView-(instanctype)initWithReuseIdentiifier:(NSString*)reuseIdentifier{self=[super initWithReuseIdentifier:reuseIdentiifier];if(self){//////////クリックジェスチャーUITapGestureRecognizer*tapGR=[[ UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickBtnBtn)@selector(clickBtn n(clickBtnBtn n):)];[self.contentView addGestureRecognizer:tapGR]; } return self; } - (void)clickBtn:sender{ !_btnClicked ?: _btnClicked(_section); } @end
  • (3)UIViewControllerで[データを表示するための配列がある]@property (nonatomic) NSArray *dataList;[tableViewパーティション数]
  •     DataModel *model = self.dataList[section]; 
       //                   
        return model.fold ? 0 : model.rowDataArr.count;
    

    [パーティションヘッダの方法において]-(UIView*)tableView:(UITableView*)tableView viewForHeaderInSection:(NSInteger)section{SectionHeaderView*v=[tableView d e q u u u e R u u s a b e H a d e r FooterViewWithIdentifier:@"SectionHeaderView";v.section=section;[v setBtnClicked:^(NSInteger section){NSLog(@"%ld",section));v.section=section;[v setBtnClicked:^(NSInteger section){NSLog(@"%ld",section))));DataModel*model=_dataList[section];//展開状態は自身の逆選択モデルに等しい.fold = !model.fold; NSMutableArray *arr = [NSMutableArray new]; for (int i = 0; i < model.rowDataArr.count; i++) { [arr addObject:[NSIndexPath indexPathForRow:i inSection:section]]; } if(model.fold){//折りたたみ、cell[tableView deleteRowsAtIndexPaths:arr withRowAnimation:UItableView RowAnimationFade];Else{//展開、すなわちcell[tableView insertRowsAtIndexPaths:arr withRowAnimation:UItableView RowAnimationFade]];return v; }