1

いくつかのセクションを持つ UITableView があります。その下の行を表示/非表示にするために、セクションをクリックしてそのコンテンツを「閉じる/開く」ようにします。そのため、一部のセクションを開いたままにして (行が表示された状態で)、他のセクションを閉じて、前のセクション ヘッダーのすぐ下に次のセクションを配置できます。

どうすれば実装できますか?UITableView をサブクラス化し、ジェスチャ認識機能を追加して、何らかの形で行にアニメーションを追加する必要がありますか? しかし、これが簡単かどうかはわかりません...

ありがとう

4

4 に答える 4

0

カスタムの展開/折りたたみビューを作成することもできる簡単なソリューションを次に示します。簡単な手順は次のとおりです。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;
            }
}
于 2016-12-02T12:30:45.810 に答える
0
  1. reloadSections:withRowAnimation変更をトリガーするために使用します。
  2. UITableViewDataSourceデリゲートで更新された行数を提供します
于 2013-08-13T07:54:06.900 に答える