2011-12-23 8 views
0

既に展開されている場合、セルを折りたたんで取得しようとしています。このコードはクリックされたときに行を展開していますが、bool cell.cell_expandedheightForRowAtIndexPathでは常にFALSEです。私は[tableView reloadData]を試しましたが、そのトリックはしませんでした。何か案が?CustomCellデータの変更をdidSelectRowAtIndexPathからheightForRowAtIndexPathに渡す方法

私はheightForRowAtIndexPathを使用してパフォーマンスに大きな影響を与える可能性があるので、可能であればこのメソッド呼び出しを削除したいと読んでいます。この機能を他の場所でリファクタリングすることは可能ですか?

CustomCell.h

@interface CustomCell : UITableViewCell 
{ 
    UIImageView *cell_arrow; 
    BOOL cell_expanded; 
} 
@property (nonatomic, strong) UIImageView *cell_arrow; 
@property (nonatomic) BOOL cell_expanded; 

FirstViewController.m

-(void)tableView:(UITableView *)tv didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    selectedCellIndexPath = indexPath; 

    CustomCell *cell = (CustomCell *)[tv cellForRowAtIndexPath:indexPath]; 

    NSString *arrow = nil; 
    if (cell.cell_expanded == TRUE) { 
     arrow = @"arrow_down.png"; 
     cell.cell_expanded = FALSE; 
    } 
    else { 
     arrow = @"arrow_up.png"; 
     cell.cell_expanded = TRUE; 
    } 
    cell.cell_arrow.image = [UIImage imageNamed:arrow]; 

    [self.tableView beginUpdates]; 
    [self.tableView endUpdates]; 
} 

-(CGFloat)tableView:(UITableView *)tv heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    CustomCell *cell = (CustomCell *)[self tableView:tableView cellForRowAtIndexPath:indexPath]; 
    if (selectedCellIndexPath != nil) { 
     if (indexPath.row == selectedCellIndexPath.row) { 
      NSLog(@"cell_expanded = %d for row %i", cell.cell_expanded, indexPath.row); 
      CGSize theSize = [cell.cell_body.text sizeWithFont:[UIFont systemFontOfSize:12.0f] constrainedToSize:CGSizeMake(265.0f, 9999.0f) lineBreakMode:UILineBreakModeWordWrap]; 
      if (cell.cell_expanded == TRUE) { 
       NSLog(@"open it"); 
       return theSize.height + 16; 
      } else { 
       NSLog(@"close it"); 
       return 44; 
      } 
     } 
     else { 
      return 44; 
     } 
    } 
    return 44; 
} 

答えて

-1

さて、ここでは物事のカップル:

をここで

は、私が持っているものです

1)-end/beginUpdatesの代わりに-reloadDataを呼び出すだけです。 2)明らかに、UITableViewの動作を実際に理解していませんでした。 UITableViewはそのセルを再利用します。これはおそらく、5つのセルインスタンスのようなものであることを意味します。あなたはおそらくそれについてdocumentsを読むべきです。問題を解決するには、展開された変数をUITableViewが表すインスタンス変数に保存する必要があります。

関連する問題