2013-02-05 58 views
0

我試圖創建一個類似的截圖應用程序:iOS:Xcode - 如何顯示單元格下的內容?

http://imgur.com/oC1yjUc

它看起來像自來水細胞透露下內容的表視圖。有誰知道如何做到這一點。我設法創建了一個單元格,其中包含我想要顯示的內容,並在加載表格時限制單元格的高度。輕拍時,高度增加。但這不是我想要的。此外,要顯示的內容可以是任何高度,所以我需要相應地調整單元格大小。

也許還有另一種做法,我試圖實現?

任何輸入將不勝感激。

編輯: 好,我得到這個工作的程度,但也有一定的問題,這一點: 1)某些行的距離展開和摺疊單元前面行 2)顯示數據內容改變,當它應該只是顯示/隱藏

我的代碼:

#import "ViewController.h" 
    #import "Data.h" 

    @interface ViewController() 

    @end 

    @implementation ViewController { 
     NSArray *data; 
     NSInteger selectedIndex; 
     NSIndexPath *selectedCellIndexPath; 
    } 

    - (void)viewDidLoad 
    { 
     [super viewDidLoad]; 
     // Do any additional setup after loading the view, typically from a nib. 

     selectedIndex = -1; 

     self.navigationItem.title = @"Test App"; 

     UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:nil action:nil]; 
     [[self navigationItem] setBackBarButtonItem:backButton]; 

     // Create Data array 
     Data *data1 = [Data new]; 
     data1.text = @"Abhijit ipsum dolor sit amet, consectetur adipiscing elit."; 
     data1.translation = @"AbhijitDE ipsum dolor sit amet, consectetur adipiscing elit."; 

     Data *data2 = [Data new]; 
     data2.text = @"Anurag ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit."; 
     data2.translation = @"AnuragDE ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit."; 

     Data *data3 = [Data new]; 
     data3.text = @"Chetan ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit."; 
     data3.translation = @"ChetanDE ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit."; 

     Data *data4 = [Data new]; 
     data4.text = @"Eric ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit."; 
     data4.translation = @"EricDE ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit."; 

     Data *data5 = [Data new]; 
     data5.text = @"Neha ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsumiraj dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit."; 
     data5.translation = @"NehaDE ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit."; 

     Data *data6 = [Data new]; 
     data6.text = @"Nilesh ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit."; 
     data6.translation = @"NileshDE ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit."; 

     Data *data7 = [Data new]; 
     data7.text = @"Niraj ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsumiraj dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit."; 
     data7.translation = @"NirajDE ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit."; 

     data = [NSArray arrayWithObjects:data1, data2, data3, data4, data5, data6, data7, nil]; 
    } 

    - (void)didReceiveMemoryWarning 
    { 
     [super didReceiveMemoryWarning]; 
     // Dispose of any resources that can be recreated. 
    } 

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

    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
    { 
     return data.count; 
    } 

    // This just a convenience function to get the height of the label based on the comment text 
    -(CGFloat)getLabelHeightForIndex:(NSString *)string 
    { 
     CGSize maximumSize = CGSizeMake(320, 10000); 

     CGSize labelHeightSize = [string sizeWithFont:[UIFont fontWithName:@"Helvetica" size:14.0f] constrainedToSize:maximumSize lineBreakMode:NSLineBreakByWordWrapping]; 

     return labelHeightSize.height + 10; 

    } 

    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
     UILabel *mainLabel, *secondLabel; 
     static NSString *CellIdentifier = @"Cell"; 
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
     if (!cell) { 
      cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 

      // First label 
      mainLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 36.00)]; 
      mainLabel.font = [UIFont fontWithName:@"Helvetica" size:14.0f]; 
      mainLabel.textColor = [UIColor blackColor]; 
      mainLabel.backgroundColor = [UIColor clearColor]; 
      mainLabel.numberOfLines = 0; 
      mainLabel.lineBreakMode = NSLineBreakByWordWrapping; 
      // mainLabel.autoresizingMask = UIViewAutoresizingFlexibleHeight; 
      // mainLabel.clipsToBounds = YES; 
      [cell.contentView addSubview:mainLabel]; 

      // Second label 
      secondLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 10.0, 320.0, 36.00)]; 
      secondLabel.font = [UIFont fontWithName:@"Helvetica" size:14.0f]; 
      secondLabel.textColor = [UIColor whiteColor]; 
      secondLabel.backgroundColor = [UIColor grayColor]; 
      secondLabel.numberOfLines = 0; 
      secondLabel.lineBreakMode = NSLineBreakByWordWrapping; 
      // secondLabel.autoresizingMask = UIViewAutoresizingFlexibleHeight; 
      // secondLabel.clipsToBounds = YES; 
      [cell.contentView addSubview:secondLabel]; 
     } 

     // Get data for the current row 
     Data *dataContent = [data objectAtIndex:indexPath.row]; 

     CGFloat dataTextHeight = [self getLabelHeightForIndex:dataContent.text]; 

     mainLabel.frame = CGRectMake(0.0, 0.0, 320.0, dataTextHeight); 
     mainLabel.text = dataContent.text; 

     CGFloat dataTranslationHeight = [self getLabelHeightForIndex:dataContent.translation]; 

     secondLabel.frame = CGRectMake(0.0, dataTextHeight + 10.00, 320.0, dataTranslationHeight); 
     secondLabel.text = dataContent.translation; 

     if(selectedIndex == indexPath.row) 
     { 
      secondLabel.hidden = YES; 
     } 
     else 
     { 
      secondLabel.hidden = YES; 
     } 

     return cell; 
    } 

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
     // Get data for the current row 
     Data *dataContent = [data objectAtIndex:indexPath.row]; 

     CGFloat dataTextHeight = [self getLabelHeightForIndex:dataContent.text]; 
     CGFloat dataTranslationHeight = [self getLabelHeightForIndex:dataContent.translation]; 

     if(selectedIndex == indexPath.row) 
     { 
      return (dataTextHeight + dataTranslationHeight) + 10; 
     } 
     else 
     { 
      return dataTextHeight; 
     } 
    } 

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
     // An expanded cell is clicked, minimize it 
     if(selectedIndex == indexPath.row) 
     { 
      selectedIndex = -1; 
      [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 

      return; 
     } 

     // If any previous cell is expanded, minimize it 
     if(selectedIndex >= 0) 
     { 
      NSIndexPath *previousPath = [NSIndexPath indexPathForRow:selectedIndex inSection:0]; 
      [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:previousPath] withRowAnimation:UITableViewRowAnimationFade]; 
     } 

     // Expand the currently expanded cell 
     selectedIndex = indexPath.row; 
     NSIndexPath *currentPath = [NSIndexPath indexPathForRow:selectedIndex inSection:0]; 
     [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:currentPath] withRowAnimation:UITableViewRowAnimationFade]; 
    } 

    @end 
+0

什麼樣的「兜底」你要幹什麼?覆蓋視圖消失,顯示的項目滑入或? – bobnoble

+0

好吧...基本上我編碼印度本地語言翻譯應用程序...基本上需要在表格視圖中顯示英語語句...點擊任何行應滑動單元格下方的翻譯...竊聽它再次應該將翻譯向後滑動...每行和翻譯的內容可以是任意長度,這意味着行的大小和翻譯應該是動態的... –

回答

0

好的。我想到了。

這裏是我更新的代碼,如果有人需要它:

#import "ViewController.h" 
    #import "Data.h" 

    @interface ViewController() 

    @end 

    @implementation ViewController { 
     NSArray *data; 
     NSInteger selectedIndex; 
    } 

    - (void)viewDidLoad 
    { 
     [super viewDidLoad]; 
     // Do any additional setup after loading the view, typically from a nib. 

     selectedIndex = -1; 

     self.navigationItem.title = @"Test App"; 

     UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:nil action:nil]; 
     [[self navigationItem] setBackBarButtonItem:backButton]; 

     // Create Data array 
     Data *data1 = [Data new]; 
     data1.text = @"Abhijit ipsum dolor sit amet, consectetur adipiscing elit."; 
     data1.translation = @"AbhijitDE ipsum dolor sit amet, consectetur adipiscing elit."; 

     Data *data2 = [Data new]; 
     data2.text = @"Anurag ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit."; 
     data2.translation = @"AnuragDE ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit."; 

     Data *data3 = [Data new]; 
     data3.text = @"Chetan ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit."; 
     data3.translation = @"ChetanDE ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit."; 

     Data *data4 = [Data new]; 
     data4.text = @"Eric ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit."; 
     data4.translation = @"EricDE ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit."; 

     Data *data5 = [Data new]; 
     data5.text = @"Neha ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsumiraj dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit."; 
     data5.translation = @"NehaDE ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit."; 

     Data *data6 = [Data new]; 
     data6.text = @"Nilesh ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit."; 
     data6.translation = @"NileshDE ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit."; 

     Data *data7 = [Data new]; 
     data7.text = @"Niraj ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsumiraj dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit."; 
     data7.translation = @"NirajDE ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit."; 

     data = [NSArray arrayWithObjects:data1, data2, data3, data4, data5, data6, data7, nil]; 
    } 

    - (void)didReceiveMemoryWarning 
    { 
     [super didReceiveMemoryWarning]; 
     // Dispose of any resources that can be recreated. 
    } 

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

    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
    { 
     return data.count; 
    } 

    -(CGFloat)getLabelHeightForIndex:(NSString *)string 
    { 
     CGSize maximumSize = CGSizeMake(320, 10000); 
     CGSize labelHeightSize = [string sizeWithFont:[UIFont fontWithName:@"Helvetica" size:14.0f] constrainedToSize:maximumSize lineBreakMode:NSLineBreakByWordWrapping]; 

     return labelHeightSize.height + 10; 
    } 

    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
     static NSString *CellIdentifier = @"Cell"; 
     static const int mainLabelTag = 1, secondLabelTag = 2; 
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

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

      // First label 
      UILabel *mainLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 36.00)]; 
      mainLabel.tag = mainLabelTag; 
      mainLabel.font = [UIFont fontWithName:@"Helvetica" size:14.0f]; 
      mainLabel.textColor = [UIColor blackColor]; 
      mainLabel.backgroundColor = [UIColor clearColor]; 
      mainLabel.numberOfLines = 0; 
      mainLabel.lineBreakMode = NSLineBreakByWordWrapping; 
      mainLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth; 
      // mainLabel.clipsToBounds = YES; 
      [cell.contentView addSubview:mainLabel]; 

      // Second label 
      UILabel *secondLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 10.0, 320.0, 36.00)]; 
      secondLabel.tag = secondLabelTag; 
      secondLabel.font = [UIFont fontWithName:@"Helvetica" size:14.0f]; 
      secondLabel.textColor = [UIColor whiteColor]; 
      secondLabel.backgroundColor = [UIColor grayColor]; 
      secondLabel.numberOfLines = 0; 
      secondLabel.lineBreakMode = NSLineBreakByWordWrapping; 
      secondLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth; 
      // secondLabel.clipsToBounds = YES; 
      [cell.contentView addSubview:secondLabel]; 
     } 

     // Get data for the current row 
     Data *dataContent = [data objectAtIndex:indexPath.row]; 

     CGFloat dataTextHeight = [self getLabelHeightForIndex:dataContent.text]; 

     UILabel *mainLabel = [cell.contentView viewWithTag:mainLabelTag]; 
     CGRect mainLabelFrame = mainLabel.frame; 
     mainLabel.frame = CGRectMake(0.0, 0.0, mainLabelFrame.size.width, dataTextHeight); 
     mainLabel.text = dataContent.text; 

     CGFloat dataTranslationHeight = [self getLabelHeightForIndex:dataContent.translation]; 

     UILabel *secondLabel = [cell.contentView viewWithTag:secondLabelTag]; 
     CGRect secondLabelFrame = mainLabel.frame; 
     secondLabel.frame = CGRectMake(0.0, dataTextHeight + 10.00, secondLabelFrame.size.width, dataTranslationHeight); 
     secondLabel.text = dataContent.translation; 

     if(selectedIndex == indexPath.row) 
     { 
      secondLabel.hidden = NO; 
     } 
     else 
     { 
      secondLabel.hidden = YES; 
     } 

     return cell; 
    } 

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
     // Get data for the current row 
     Data *dataContent = [data objectAtIndex:indexPath.row]; 
     CGFloat dataTextHeight = [self getLabelHeightForIndex:dataContent.text]; 
     CGFloat dataTranslationHeight = [self getLabelHeightForIndex:dataContent.translation]; 

     if(selectedIndex == indexPath.row) 
     { 
      return (dataTextHeight + dataTranslationHeight) + 10; 
     } 
     else 
     { 
      return dataTextHeight; 
     } 
    } 

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {  
     // An expanded cell is clicked, minimize it 
     if(selectedIndex == indexPath.row) 
     { 
      selectedIndex = -1; 
      [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; 
      return; 
     } 

     // If any previous cell is expanded, minimize it 
     if(selectedIndex > -1) 
     { 
      NSIndexPath *previousPath = [NSIndexPath indexPathForRow:selectedIndex inSection:0]; 
      selectedIndex = -1; 
      [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:previousPath] withRowAnimation:UITableViewRowAnimationAutomatic]; 
     } 

     // Expand the currently expanded cell 
     selectedIndex = indexPath.row; 
     NSIndexPath *currentPath = [NSIndexPath indexPathForRow:selectedIndex inSection:0]; 
     [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:currentPath] withRowAnimation:UITableViewRowAnimationAutomatic]; 
    } 

    @end 
-1

也許你可以試試

cell.clipsToBounds = YES; 
cell.autoresizingMask = UIViewAutoresizingFlexibleHeight; 

+0

單元格不會以這種方式調整大小。你需要重寫'UITableViewDelegate'方法'tableView:heightForRowAtIndexPath:'。 –

+0

我知道,你顯然必須重寫這種方法,但這種方式(組合)可以更容易地改變它。看看例如HTTP://計算器。com/questions/10220565/expanding-uitableviewcell請在投票前仔細檢查下來的答案 – blub

+0

我想我已經嘗試過這之前...我會嘗試發佈源代碼在這裏,甚至項目供參考...... –

1

動態大小的細胞是一個疼痛的屁股,然後一些。查看它,因爲StackOverflow上有很多關於它的問題和一些有趣的解決方案。

就底部視圖而言,有一個很好的開源項目叫HHPanningTableViewCell,你應該看看。我們已經用於我們的幾個項目,它有訣竅。不幸的是,有一些性能問題(請參閱我在項目中的公開問題),所以請確保您在設備上進行測試。

+0

好的貢獻,感謝您讓我們瞭解HHPanningTableViewCell。 Jim – JimP

+0

這看起來相當整潔...不是我目前需要的東西,但絕對是我將來可以使用的東西... –

0

在表視圖委託tableView:didSelectRowAtIndexPath:方法,使用insertRowsAtIndexPaths:withRowAnimation:deleteRowsAtIndexPaths:withRowAnimation:方法當「源語言」小區被分接,與所插入的細胞作爲「目的地語言」插入一個新的小區。當用戶再次點擊時,刪除「目標語言」單元格。在這兩種方法中,您希望的揭示動畫都可以指定。

+0

謝謝...我使用'reloadRowsAtIndexPaths'來重新加載行以顯示隱藏的標籤內容...但我遇到了一些問題...請參閱上面的代碼... –

相關問題