20

カスタム セルのテーブル ビューと、各セルにいくつかのボタンがあります。セル内のボタンのいずれかをクリックすると、そのセルの下に別のカスタム ビューが表示されます。次に同じボタンをクリックすると、ビューが折りたたまれ、すべてのセルに同じものが必要になります。 .ボタンクリックでinsertrowメソッドを試してみましたが、無駄でした.テーブルビューデリゲートのみを使用してこれを行うにはどうすればよいですか.

これは私が試したものです:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"CustomCell_For_Dashboard";

    CustomCellFor_Dashboard  *customCell = (CustomCellFor_Dashboard *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    if (customCell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCellFor_Dashboard" owner:self options:nil];
        customCell = [nib objectAtIndex:0];
    }
    [customCell.howyoulfeelBtn  addTarget:self action:@selector(buttonclicked:) forControlEvents:UIControlEventTouchUpInside];
      customCell.nameLabel.text = @"test";
    customCell.imgView.image = [UIImage imageNamed:@"Default.png"];
   // customCell.prepTimeLabel.text = [prepTime objectAtIndex:indexPath.row];
    return customCell;
}

-(void)buttonclicked:(id)sender{
    NSIndexPath *indexPath = [myTable indexPathForCell:sender];


    [myTable beginUpdates];

     NSIndexPath *insertPath = [NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section];
   [myTable insertRowsAtIndexPaths:[NSArray arrayWithObject:insertPath] withRowAnimation:UITableViewRowAnimationTop];
  } 

誰でも私を助けることができますか?

4

4 に答える 4

33

私は 1 つのプロジェクトで同じタスクを取得しましたが、1 つだけ違いがあります。ボタンがなく、セルをタップするだけでセルが展開または折りたたまれます。

コードで編集する必要があるものがいくつかあります。まず、ボタン メソッドのコードは次のようになります。

- (void) collapseExpandButtonTap:(id) sender
{
    UIButton* aButton = (UIButton*)sender; //It's actually a button
    NSIndexPath* aPath = [self getIndexPathForCellWithButtonByMagic:aButton];
    //expandedCells is a mutable set declared in your interface section or private class extensiont
    if ([expandedCells containsObject:aPath])
    {
        [expandedCells removeObject:aPath];
    }
    else
    {
        [expandedCells addObject:aPath];
    }
    [myTableView beginEditing];
    [myTableView endEditing]; //Yeah, that old trick to animate cell expand/collapse
}

次にUITableViewDelegateメソッドです。

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([expandedCells containsObject:indexPath])
    {
        return kExpandedCellHeight; //It's not necessary a constant, though
    }
    else
    {
        return kNormalCellHeigh; //Again not necessary a constant
    }
}

ここで重要なことは、セルを展開/折りたたむ必要があるかどうかを判断し、デリゲート メソッドで正しい高さを返すことです。

于 2013-11-08T10:56:57.580 に答える
17

@eagle.dan.1349が言ったことから外れて、これはセルのクリックでそれを行う方法です。ストーリーボードでは、サブビューをクリップするようにテーブル セルを設定する必要もあります。そうしないと、非表示になるコンテンツが表示されます。

.h

@property (strong, nonatomic) NSMutableArray *expandedCells;

.m

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    if ([self.expandedCells containsObject:indexPath])
    {
        [self.expandedCells removeObject:indexPath];
    }
    else
    {
        [self.expandedCells addObject:indexPath];
    }
    [tableView beginUpdates];
    [tableView endUpdates];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat kExpandedCellHeight = 150;
    CGFloat kNormalCellHeigh = 50;

    if ([self.expandedCells containsObject:indexPath])
    {
        return kExpandedCellHeight; //It's not necessary a constant, though
    }
    else
    {
        return kNormalCellHeigh; //Again not necessary a constant
    }
}
于 2014-05-09T16:32:47.067 に答える
0

私も同じ状況にあり、私の解決策は、viewForHeaderInSectionメソッドを使用してセクション タイトルの上にボタンを配置することでした。

noOfRows各セクションにいくつの行があるかを定義し、button.tagセクションのどのボタンが押されたかを保持します。

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIButton *btnSection = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, tableView.frame.size.height)];
    btnSection.tag = section;   
    [btnSection setTitle:[sectionArray objectAtIndex:section] forState:UIControlStateNormal];
    [btnSection addTarget:self action:@selector(sectionButtonTapped:) forControlEvents:UIControlEventTouchUpInside];

    return btnSection;
} 

- (void)sectionButtonTapped:(UIButton *)button {
    sectionIndex = button.tag;
    if (button.tag == 0) {
        noOfRows = 3;
    } else if (button.tag == 1) {
        noOfRows = 1;
    } else if (button.tag == 2) {
        noOfRows = 2;
    }

    [self.tableView reloadData];
}

これがあなたを助けることを願っています..

于 2016-10-13T11:31:45.003 に答える