2016-04-30 20 views
1

Ich entwickle eine iOS-Anwendung. Ich habe eine erweiterbare und minimierte Tabellenansicht. Wenn Sie auf die Zelle von tableview klicken, wird die Tabellenansichtszelle ordnungsgemäß vergrößert, aber wenn ich auf eine zweite Zelle klicke, möchte ich, dass die erste Zelle minimiert wird und die zweite Zelle erweitert wird. Dadurch würde jeweils nur eine Zelle erweitert.Erweitern und reduzieren Sie die Tabellenansicht Zeile

Ich bin mit Klasse HVTableview: - Code: -

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

Möchten Sie die Zeilenhöhe beim Kollaps reduzieren? – Lion

+0

Nein Ich möchte das letzte Element ausblenden. Wenn Sie auf die zweite Zelle klicken, möchte ich jeweils eine Zelle erweitern. –

+0

Was meinst du mit letzter Artikel? – Lion

Antwort

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 
} 

In UITableViewDelegate Methode:

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

Key Sache hier ist, um zu bestimmen, ob Ihr Handy erweitert werden soll/collapsed und gibt die richtige Höhe in der Delegate-Methode zurück. Entwerfen Sie dementsprechend Ihre Anforderungen.

+0

Wenn Sie dies bei der Zellenauswahl tun möchten, wenden Sie die Logik in der Tabelle an - (void) tableView: (UITableView *) tableView didSelectRowAtIndexPath: (NSIndexPath *) indexPath {// here} – vivek

Verwandte Themen