カスタムの展開/折りたたみビューを作成することもできる簡単なソリューションを次に示します。簡単な手順は次のとおりです。1) その上にカスタム ビューの追加ボタンを作成します。
/// すべてのアウトレットに参加し、ビュー クラスで BOOL 変数を作成します
@property (weak, nonatomic) IBOutlet UIButton *BtnAction;
@property(assign, nonatomic)BOOL isOpen;
// tableview が追加され、必要なヘッダーを作成します。これは、必要なだけ追加する簡単なロジックです。動的にしたいヘッダータイトル配列にあるものを追加しました。
NSMutableArray * headerTitle = [NSMutableArray arrayWithObjects:@"Your Order", @"Delivery Address", @"Pay By", nil];
for (NSUInteger index = 0; index<headerTitle.count; index++) {
VGOrderHeader* HeaderView = [[[NSBundle mainBundle] loadNibNamed:@"VGOrderHeader" owner:self options:nil] lastObject];
HeaderView.frame = CGRectMake(0, 0, 32, 40);
HeaderView.BtnAction.tag = index;
if (index == 0) {
HeaderView.isOpen = YES;
HeaderView.lblPlus.text = [NSString stringWithFormat:@"open"];
}
[HeaderView.BtnAction addTarget:self action:@selector(selectSectionToOpen:) forControlEvents:UIControlEventTouchUpInside];
[headerArray addObject:HeaderView];
}
/// ここにヘッダーのクリック アクションがあります。
-(void)selectSectionToOpen:(UIButton *)sender{
for (NSUInteger Increment=0; Increment<headerArray.count; Increment++) {
if (sender.tag == Increment) {
DCOrderHeader* HeaderView= headerArray[Increment];
HeaderView.isOpen = !HeaderView.isOpen;
}
}
// little animation
dispatch_async(dispatch_get_main_queue(), ^{
[UIView transitionWithView:self.tableView
duration:0.55f
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^(void) {
[self.tableView reloadData];
} completion:NULL];
});
}
/// 最後に、テーブル ビューのヘッダー メソッドでビューを割り当て、高さを指定します
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 40;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
return [headerArray objectAtIndex:section];
}
// ファイナルタッチ
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return headerArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
DCOrderHeader* HeaderView = headerArray[section];
if (HeaderView.isOpen == YES) {
return self.someArray.count;
}else{
return 0;
}
}