2016-04-30 11 views
1

iOSアプリケーションを開発中です。私は1つの拡張可能なテーブルビューと折り畳まれたテーブルビューを持っています。テーブルビューのセルをクリックすると、テーブルビューのセルは適切に展開されますが、2番目のセルをクリックすると、最初のセルが折りたたまれ、2番目のセルが展開されます。これにより、一度に1つのセルのみが展開されたままになります。私は、クラスHVTableviewを使用していますテーブルビュー行を展開して折り畳む

: - コード: - UITableViewDelegate方法で

-(void)tableView:(UITableView *)tableView expandCell:   (UITableViewCell*)cell withIndexPath:(NSIndexPath*) indexPath; 
{ 
UILabel *detail = (UILabel *)[cell viewWithTag:3]; 
UIButton *button = (UIButton *)[cell viewWithTag:10]; 
button.alpha = 0; 
button.hidden = NO; 

[UIView animateWithDuration:.5 animations:^{ 
    detail.text = @"This is Expand."; 
    button.alpha = 1; 
    [cell.contentView viewWithTag:7].transform = CGAffineTransformMakeRotation(3.14); 
}]; 

} 

-(void)tableView:(UITableView *)tableView collapseCell: (UITableViewCell*)cell withIndexPath:(NSIndexPath*) indexPath; 
{ 
UILabel *detail = (UILabel *)[cell viewWithTag:3]; 
UIButton *button = (UIButton *)[cell viewWithTag:10]; 
button.alpha = 0; 

[UIView animateWithDuration:.5 animations:^{ 
    detail.text = @"This is Collapse."; 
    button.alpha = 0; 
    [cell.contentView viewWithTag:7].transform = CGAffineTransformMakeRotation(-3.14); 
} completion:^(BOOL finished) { 
    button.hidden = YES; 
}]; 


} 
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
return 1; 
} 

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
return [_cites count]; 
} 
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath isExpanded: (BOOL)isExpanded; 
{ 
if (isExpanded) { 
    return 150; 
    }else 
    { 
    return 100; 
    } 

} 
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath isExpanded:(BOOL)isExpanded; 
{ 
static NSString *CellIdentifier = @"cell"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if(cell == nil) 
{ 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 

} 
UILabel *title = (UILabel *)[cell viewWithTag:2]; 
UILabel *detail = (UILabel *)[cell viewWithTag:3]; 
UIImageView *image = (UIImageView *)[cell viewWithTag:1]; 
UIButton *button = (UIButton *)[cell viewWithTag:10]; 
[button addTarget:self action:@selector(goToTop) forControlEvents:UIControlEventTouchUpInside]; 
title.text = [_cites objectAtIndex:indexPath.row % 9]; 
NSString* bundlePath = [[NSBundle mainBundle] bundlePath]; 
NSString* imageFileName = [NSString stringWithFormat:@"%d.jpg", indexPath.row % 9 + 1]; 
image.image = [[UIImage alloc] initWithContentsOfFile:[NSString stringWithFormat:@"%@/%@", bundlePath, imageFileName]]; 


if (indexPath.row %2 ==1) 
    cell.backgroundColor = [UIColor colorWithRed:.9 green:.9 blue:.9 alpha:1]; 
else 
    cell.backgroundColor = [UIColor colorWithRed:.8 green:.8 blue:.8 alpha:1]; 
if (!isExpanded) 
{ 
    detail.text = @"This is collapse"; 
    [cell.contentView viewWithTag:7].transform = CGAffineTransformMakeRotation(0); 
    button.hidden = YES; 
} 
else 
{ 
    detail.text = @"This is Expand"; 
    [cell.contentView viewWithTag:7].transform = CGAffineTransformMakeRotation(3.14); 
    button.hidden = NO; 
} 


    return cell; 
} 
+0

あなたは崩壊時に行の高さを減らしたいですか? – Lion

+0

いいえ最後のアイテムを折りたたんでいます.2番目のセルをクリックすると、一度に1つのセルを展開します。 –

+0

最後のアイテムはどういう意味ですか? – Lion

答えて

0
- (void) collapseExpandButtonTap:(id) sender 
{ 
    UIButton* aButton = (UIButton*)sender; //It's actually a button 
    NSIndexPath* aPath = [self indexPathForCellWithButtonTag:aButton.tag]; //Let's assume that you have only one section and button tags directly correspond to rows of your cells. 
    //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 
} 

:ここ

- (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 
    } 
} 

重要なことは、あなたのセルを拡張する必要があるかどうかを判断することです/デリゲートメソッドで折りたたんで右の高さを返します。したがって、要件を設計します。

+0

セル選択でそれを行う場合は、 - (void)tableView:(UITableView *)のロジックを適用します。tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {//ここ} – vivek

関連する問題