Я пытаюсь разрушить ячейку, если она уже была расширена. Этот код расширяет строку при нажатии, но bool cell.cell_expanded
всегда ложно в heightForRowAtIndexPath
. Я пробовал [tableView reloadData]
, но это не помогло. Есть идеи?
Я читал, что использование 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;
}