2016-04-30 194 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