0

I have a custom table view & cell where a cell is expanded when selected. It is now functioning properly and accordingly. However, When I select cells to expand them, it takes about half a second to respond. The code below is located in the didSelectRowAtIndexPath. My hypothesis is that between beginUpdates and endUpdates, there are too many things going on to increase the height of the original cell and then updating the whole table view. Is there another way I can better implement this?

**[_tableView beginUpdates];**

ReviewTestTableCell *reviewCell1 = (ReviewTestTableCell *)[tableView cellForRowAtIndexPath:indexPath];

        CGRect rect = CGRectMake(reviewCell1.review.width, 900, reviewCell1.review.width, 900);

        CGRect textRectb = [reviewCell1.review textRectForBounds:rect limitedToNumberOfLines:1000];
        float labelHeight = textRectb.size.height;

        reviewCell1.review.height = labelHeight;
        expandHeight = labelHeight + 75 ;

        if ([[userdefaults objectForKey:@"merchantType"] isEqual:@"T"])
        {reviewCell1.height = labelHeight + 50;
        reviewCell1.bottomRatingView.height = reviewCell1.bottomRatingView.height - 20;

        }
        else
        {
            reviewCell1.height = labelHeight + 75;}

        reviewCell1.bottomRatingView.hidden = NO;
        reviewCell1.bottomRatingView.top = reviewCell1.review.bottom;

        **[_tableView endUpdates];**

       [_isExpandList replaceObjectAtIndex:indexPath.row withObject:@1];
    }

EDIT/ADD:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

 UITableViewCell *cell3 = [self tableView:tableView cellForRowAtIndexPath:indexPath];

if ([_isExpandList[indexPath.row]  isEqual: @1] && [[_dataList objectAtIndex:indexPath.row] valueForKey:@"Item1Score"] != nil) {

           return  cell3.height + 3 + 65;
}
else if ([_isExpandList[indexPath.row]  isEqual: @0])
{
return cell3.height +5;
}
else return cell3.height +3;
}

1 Answers1

0

You should not go through that elaborate dance of trying to manually expand the cell. You should certainly not manually call willDisplayCell.

Using the method described in the answer to this question, you should have a property or something to keep track of which cell was selected and make your heightForRowAtIndexPath: method adjust for that particular indexPath, then just call

[tableView beginUpdates];
[tableView endUpdates];

Which will call heightForRowAtIndexPath for every cell, which your method will give a larger height for when it matches the selected row. The tableView will smoothly adjust the height of your cell.

Something similar to:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([self.selectedIndexPath isEqual:indexPath]) {
        return 80.0f;
    }

    return tableView.rowHeight;
}
Community
  • 1
  • 1
Neil
  • 1,813
  • 1
  • 12
  • 20
  • Thank you for helping, I've removed willDisplayCell and updated HeightForRowIndexPath, but it is still not smooth. Is there something wrong with my if statement in heightForRowIndexPath? – user1992596 Apr 30 '14 at 00:06