2016-04-30 196 views
1

我正在开发iOS应用程序。我有一个可扩展和折叠的tableview。在点击tableview的单元格时,tableview单元格会正确展开,但当我点击第二个单元格时,我希望第一个单元格可以折叠,第二个单元格可以展开。这样一次只能扩展一个单元。展开并折叠tableview行

我使用类HVTableview: - 代码: -

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

不,我想折叠最后一项。当点击第二个单元格时,我想一次展开一个单元格。 –

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

在的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 
    } 
} 
这里

关键是要确定您的电池应该扩大/折叠并在委托方法中返回正确的高度。所以相应地设计你的要求。

+0

如果你想在单元格选择中做到这一点,然后应用 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {// here} – vivek