Changing a specific uitableviewcell row height while scrolling a uitableview
Asked Answered
Y

1

7

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?

Yolandayolande answered 3/2, 2012 at 8:40 Comment(0)
Y
9

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);
    }
}
Yolandayolande answered 3/2, 2012 at 22:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.