Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing a specific uitableviewcell row height while scrolling a uitableview

I have a UITableview with 2 sections, 1 row in the 1st section, and a variable amount of rows for the second section. When the user scrolls the table up (finger moving upwards), then the tableview does it's default thing, but when the user is at the top of the uitableview and scrolls down (finger moving downwards) then it should look like the first cell in the first section height increases as much as the user scrolls down (and releasing the touch will change the height of the row back to it's original height of 100). I tried

-(void)scrollViewDidScroll:(UIScrollView*)scrollView{
    if(scrollView.contentOffset.y < 0){
        [mainTable reloadRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection: 0]] withRowAnimation:UITableViewRowAnimationNone];
    }
}

-(CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath{
    if(indexPath.section == 0 && tableView.contentOffset.y < 0){
        return -tableView.contentOffset.y+100;//Default height is 100
    }
}

but the reloadRows method resets the contentOffset to 0 so the tableview just stutters when i pull down. It seems like if this doesn't work, then i'll have to write everything on a UIScrollView and it seems like a huge pain without recyclable cells.

Does anybody have any suggestions?

like image 201
Andrew Park Avatar asked Feb 03 '12 08:02

Andrew Park


1 Answers

No need to reload the row, i just changed the frame directly. Make sure you set the cell.layer.masksToBounds = YES and make the subViews in the cell to be larger than the original cell height.

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (mainTable.contentOffset.y < 0) {
        UITableViewCell *cell = [mainTable cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
        float rawr = mainTable.contentOffset.y;
        cell.frame = CGRectMake(cell.frame.origin.x, rawr, cell.frame.size.width, 100-rawr);
    }
}
like image 58
Andrew Park Avatar answered Oct 02 '22 18:10

Andrew Park