2012-06-12 72 views
2

我有UITableView使用commentTableCell它的高度約爲200像素,它有文字和圖像。UITableView擴展和摺疊顯示部分或完全單元格

我希望當一行沒有被選中,那麼該行的高度約爲65像素,這將隱藏所有的圖像,當行被擴大到200像素,那麼commentTableCell將完全展開,因此內部的圖像細胞會暴露。

但我遇到的問題是,當所有行都被摺疊到65像素的高度,沒有任何來自commentTabelCell的圖像應該顯示,但我從表格的某些部分顯示的單元格中獲取鬼影或部分圖像。爲什麼?

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

     CommentTableCell *cell = (CommentTableCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

     return cell;  
} 

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
     if(selectedIndex == indexPath.row) 
     { 

      return 200; 
     } 
     else { 

      return 65; 
     } 
} 

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

     //The user is selecting the cell which is currently expanded 
     //we want to minimize it back 
     if(selectedIndex == indexPath.row) 
     { 
      selectedIndex = -1; 
      [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 

      return; 
     } 

     //First we check if a cell is already expanded. 
     //If it is we want to minimize make sure it is reloaded to minimize it back 
     if(selectedIndex >= 0) 
     { 
      NSIndexPath *previousPath = [NSIndexPath indexPathForRow:selectedIndex inSection:0]; 
      selectedIndex = indexPath.row; 
      [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:previousPath] withRowAnimation:UITableViewRowAnimationFade];   
     } 

     //Finally set the selected index to the new selection and reload it to expand 
     selectedIndex = indexPath.row; 
     [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
} 

回答

3

我有一個替代解決方案。試試這個:

// ----------------------------// 

// Put this somewhere in your .h file: 
NSIndexPath *selectedCellIndexPath; 

// ----------------------------// 

// Set your default row height 
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
    return 65; 
} 

// In the implementation file: 
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    selectedCellIndexPath = indexPath; 

    // Forces the table view to call heightForRowAtIndexPath 
    [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone]; 
} 

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
    // Note: Some operations like calling [tableView cellForRowAtIndexPath:indexPath] 
    // will call heightForRow and thus create a stack overflow 
    if(selectedCellIndexPath != nil && [selectedCellIndexPath compare:indexPath] == NSOrderedSame) 
     return 200; 

    return 65; 
} 
+0

好,簡單的回答,Kimpoy。 –