Search code examples
objective-ciosuitableviewframecell

Change UITableViewCell frame but didn't work


UITableViewCell *cell = [tableViewController.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]];

[cell setFrame:CGRectMake(cell.frame.origin.x, cell.frame.origin.y - 34, cell.frame.size.width, cell.frame.size.height)];

I want to change the frame of table view cell, the code work in some ViewController, but some it doesn't work. I want to know is there something wrong with it.

thanks a lot.


Solution

  • You cannot have full control on the cell frame, especially on the origin. The table view delegate is asked for a table view cell, but then its position depends on the internal logic of the table view in order to correctly place the cell inside the table view (which is a scroll view subclass). When the displayed portion of the table view is refreshed, the cell position is rearranged and the frame origin is updated. So while your code is not wrong, it conflicts with some table internal rules you cannot fully control and so the results could unpredictable.

    If you want instead to see a visible effect of this change, you can move your frame changer code to another part of the code, e.g. inside the

    tableView:didSelectRowAtIndexPath:
    In such case you can for example touch a cell, call this code and change the cell frame and you will the change immediately. But as soon as you scroll the cell out of the iPhone screen, and then you move back this cell in the screen, you will notice that it will re-appear in its original position.