2013-03-23 7 views
8

Chcę powiększyć rozmiar komórki po kliknięciu przycisku seeMoreBtn w komórce.Jak zwiększyć etykietę i rozmiar komórki po kliknięciu przycisku w TableViewCell.

Etykieta i komórki mają różne długości, ale ich jest ograniczenie wielkości etykiety.

Gdy status jest zbyt duży, dodałem seeMoreBtn, po kliknięciu, zobacz więcej pozostały tekst zostanie wyświetlony poniżej, a następnie jak zwiększyć etykietę i rozmiar komórki.

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath; 
    { 


     NSString *text = [items objectAtIndex:[indexPath row]]; 
     CGSize constraint = CGSizeMake(300.0f, 150.0f); 
     CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:14.0f] constrainedToSize:constraint lineBreakMode:NSLineBreakByCharWrapping]; 

     CGFloat height1 = MAX(size.height, 44.0f); 
     return height1 + (40.0f); 
     } 
    - (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
     NSString *CellIdentifier = [NSString stringWithFormat:@"Cell-%d",indexPath.row]; 

     cell=[tv dequeueReusableCellWithIdentifier:CellIdentifier]; 

     if (cell == nil) 
     { 
      cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
     } 

     int lbltag = 1000; 

     label=[[UILabel alloc]initWithFrame:CGRectZero]; 
     [label setLineBreakMode:NSLineBreakByWordWrapping]; 
     [label setMinimumScaleFactor:14.0f]; 
     [label setNumberOfLines:0]; 
     [label setFont:[UIFont systemFontOfSize:14.0f]]; 
     NSString *text = [items objectAtIndex:[indexPath row]]; 
     [label setText:text]; 
     label.tag = lbltag; 
     [cell addSubview:label]; 

     CGSize constraint1=CGSizeMake(300.0f, 150.0f); 
     CGSize size1=[text sizeWithFont:[UIFont systemFontOfSize:14.0f] constrainedToSize:constraint1 lineBreakMode:NSLineBreakByWordWrapping]; 


     [label setFrame:CGRectMake(10.0f, 10.0f, 300.0f, MAX(size1.height, 44.0f))]; 

     int countText=text.length; 

     if (countText>=350) { 
      seeMoreBtn=[UIButton buttonWithType:UIButtonTypeRoundedRect]; 
      [seeMoreBtn setTitle:@"See More" forState:UIControlStateNormal]; 
      seeMoreBtn.frame=CGRectMake(220.0f, MAX(size1.height, 44.0f)-10, 80.0f, 20.0f); 
      seeMoreBtn.tag=indexPath.row ; 
       [seeMoreBtn addTarget:self action:@selector(increaseSize:) forControlEvents:UIControlEventTouchUpInside]; 
      [cell addSubview:seeMoreBtn];    
     } 
     return cell; 
    } 

    -(void)increaseSize:(UIButton *)sender{ 
//What to write here that can adjust the label and cell size 

    } 

Odpowiedz

5

Byłoby lepiej, jeśli podklasy UITableViewCell i użyj layoutSubviews dostosować podczas regulacji wielkości komórki.

//In SMTableViewCell.h 

@interface SMTableViewCell : UITableViewCell 

@property (weak, nonatomic) IBOutlet UILabel *statusLabel; 
@property (weak, nonatomic) IBOutlet UIButton *seeMoreButton; 

//SMTableViewCell.m 

- (void)layoutSubviews 
{ 
    CGRect labelFrame = self.statusLabel.frame; 
    labelFrame.size.height = self.frame.size.height - 55.0f; 
    self.statusLabel.frame = labelFrame; 

    CGRect buttonFrame = self.seeMoreButton.frame; 
    buttonFrame.origin.y = labelFrame.origin.y+labelFrame.size.height+10.0f; 
    self.seeMoreButton.frame = buttonFrame; 
} 

przechowywać tablicę do przechowywania selectedIndexPaths:

@property (nonatomic, strong) NSMutableArray *selectedIndexPaths; 

obliczyć wysokość komórki:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath; 
{ 
    BOOL isSelected = [self.selectedIndexPaths containsObject:indexPath]; 

    CGFloat maxHeight = MAXFLOAT; 
    CGFloat minHeight = 40.0f; 

    CGFloat constrainHeight = isSelected?maxHeight:minHeight; 
    CGFloat constrainWidth = tableView.frame.size.width - 20.0f; 

    NSString *text  = self.items[indexPath.row]; 
    CGSize constrainSize = CGSizeMake(constrainWidth, constrainHeight); 
    CGSize labelSize  = [text sizeWithFont:[UIFont systemFontOfSize:15.0f] 
          constrainedToSize:constrainSize 
           lineBreakMode:NSLineBreakByCharWrapping]; 

    return MAX(labelSize.height+75, 100.0f); 

} 

Inicjuj zwyczaj Pokaż więcej TableViewCell:

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

    SMTableViewCell *cell= (SMTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 

    if (cell == nil) 
    { 
     cell = [[[NSBundle mainBundle]loadNibNamed:NSStringFromClass([SMTableViewCell class]) 
              owner:nil 
              options:nil] lastObject]; 
    } 

    BOOL isSelected = [self.selectedIndexPaths containsObject:indexPath]; 
    cell.statusLabel.numberOfLines = isSelected?0:2; 

    NSString *text = self.items[indexPath.row]; 
    cell.statusLabel.text = text; 


    NSString *buttonTitle = [email protected]"See Less":@"See More"; 
    [cell.seeMoreButton setTitle:buttonTitle forState:UIControlStateNormal]; 
    [cell.seeMoreButton addTarget:self action:@selector(seeMoreButtonPressed:) forControlEvents:UIControlEventTouchUpInside]; 
    [cell.seeMoreButton setTag:indexPath.row]; 

    cell.selectionStyle = UITableViewCellSelectionStyleNone; 

    return cell; 
} 

przycisk kliknij wydarzenie Metoda:

- (void)seeMoreButtonPressed:(UIButton *)button 
{ 
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:button.tag inSection:0]; 
    [self addOrRemoveSelectedIndexPath:indexPath]; 
} 

- (void)addOrRemoveSelectedIndexPath:(NSIndexPath *)indexPath 
{ 
    if (!self.selectedIndexPaths) { 
     self.selectedIndexPaths = [NSMutableArray new]; 
    } 

    BOOL containsIndexPath = [self.selectedIndexPaths containsObject:indexPath]; 

    if (containsIndexPath) { 
     [self.selectedIndexPaths removeObject:indexPath]; 
    }else{ 
     [self.selectedIndexPaths addObject:indexPath]; 
    } 

    [self.tableView reloadRowsAtIndexPaths:@[indexPath] 
        withRowAnimation:UITableViewRowAnimationFade]; 

} 

samo zdarzenie jest podana, jeśli komórka jest zaznaczona:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [tableView deselectRowAtIndexPath:indexPath animated:YES]; 

    [self addOrRemoveSelectedIndexPath:indexPath]; 

} 

Przykładowe Demo project link.

0

Cóż, chodzi o to, aby zmienić wysokość komórki, na podstawie wartości ustawionej dla komórki. Możesz mieć NSArray, gdzie po prostu zaznacz dla wszystkich komórek wartość: isExpanded (true lub false).

Podsumowując, należy utworzyć NSMutableArray w którym będzie przechowywany stan komórek (rozszerzony lub nie):

NSMutableArray *cellState; 

startowych tablicy i wypełnij go zerami wszystkich elementów UITableView Array. Po rozwinięciu komórki ustaw wartość cellState objectAtIndex z 0 na 1 (oznaczając ją jako rozwiniętą).

Następnie przeładować że komórka:

[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section]] withRowAnimation:UITableViewRowAnimationFade]; 

i zmodyfikować funkcję - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath; zwrócić większą wartość, jeśli komórka jest rozszerzony ...

To jest to!

1

dodać obiekt jak:

@property(strong, nonatomic) NSArray *enlargedIndexPaths; 

zainicjować do pustej tablicy. Wdrożenie delegata widok tabeli:

- (CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 

    return ([self.enlargedIndexPaths containsObject:indexPath])? 88.0 : 44.0; 
} 

Następnie w increaseSize:

UITableViewCell *cell = sender.superview; 
NSIndexPath *indexPath = [self.tableView indexPathFoCell:cell]; 
[self.enlargedIndexPaths addObject:indexPath]; 

// do you want to enlarge more than one at a time? You can remove index paths 
// here, too. Just reload them below 
[self.tableView beginUpdates]; 
[self.tableView reloadRowsAtIndexPaths:self.enlargedIndexPaths] withRowAnimation:UITableViewRowAnimationTop]; 
[self.tableView endUpdates]; 
Powiązane problemy