既に展開されている場合、セルを折りたたんで取得しようとしています。このコードはクリックされたときに行を展開していますが、bool cell.cell_expanded
はheightForRowAtIndexPath
では常に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;
}